Moving from java to python.

Hrvoje Niksic hniksic at xemacs.org
Mon Nov 12 10:42:27 EST 2007


"PeterBraden1 at googlemail.com" <PeterBraden1 at googlemail.com> writes:

> 	def __init__(self, connections = None, uid = None):

You can use connections=(), so you don't need the "if" below.  In
fact, you can further write:

    for node, weight in connections:
        self._connected.append(node)
        self._weight.append(weight)

> 	def getConnected(self):
> 		return self._connected

I'd drop the getters and simply use self.connected, self.weights, etc.
It's not such a big deal in Python if someone can view the innards of
your objects; they can do so even if their name starts with _.

Also note that Python doesn't really use the camel-case naming
convention.  If you must have multi-word method/variable/property
names, use lower-case with underscores for separation.  For example:

> 	def getConnections(self):

Why not simply def connections(self) or, if you must, def
get_connections(self).

> 		connections = []
> 		for i in range(len(connected)):
> 			connections.append((self._connected[i],self._weight[i]))

Use xrange(len(...)) when iterating over the sequence; range
unnecessarily allocates the whole list.  Some will probably recommend
enumerate() or even itertools.izip(), but those are overkill for this
usage.



More information about the Python-list mailing list