Moving from java to python.

Hrvoje Niksic hniksic at
Mon Nov 12 16:42:27 CET 2007

"PeterBraden1 at" <PeterBraden1 at> 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:

> 	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

> 		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

More information about the Python-list mailing list