Question

I'm extending a former colleague's code (Python3) and find personally those repeated self.__local_object.x=some_result annoying and hindering readabilit. I.e. instead of

 self.__local_node.sign("computing partition for joining node %f<?<%f"%(
        self.__local_node.partition_id,next_neighbour.partition_id))
    partition_id = 0
    if(next_neighbour != self.__local_node):
        partition_id = PartitionID.gen_btw(self.__local_node.partition_id, next_neighbour.partition_id)

I'd rather use

 ln=self.__local_node
 ln.sign("computing partition for joining node %f<?<%f"%(
     ln.partition_id,next_neighbour.partition_id))
 partition_id = 0
 if(next_neighbour != ln):
     partition_id = PartitionID.gen_btw(ln.partition_id, next_neighbour.partition_id)

However, I'm not yet seasoned with Python development and I may be missing a golden guideline that will make further maintenance a nightmare if I introduce such (hopefully) local aliases for referenced objects.

PS: no, self.__local_node's value is not altered any place in that code.

Was it helpful?

Solution

If you really don't change the value, only assingn or access its attributes, this should be perfectly ok.

OTHER TIPS

Since local_node and next_neighbour are symmetrical, it might be a good idea to extract the code into a separate method:

def compute_partition(self, a, b):
    a.sign("computing partition for joining node %f<?<%f" % (
        a.partition_id, b.partition_id))
    partition_id = 0
    if a != b:
        partition_id = PartitionID.gen_btw(a.partition_id, b.partition_id)
    etc...

....

self.compute_partition(self.local_node, next_neighbour)

a bit more readable in my view. Also, there must be very good reasons (or excuses) for using double underscores. Check if it's possible to get rid of them in your case.

I wouldn't be surprised if all the members of the __local_node object used the belong to the class referenced here as self, and this is the product of a copy and paste rampage.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top