Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why give a local variable an initial value immediately before assigning to it?

Tags:

python

While looking at the source code of the asyncore module I came across this method. I'll include it here without context, as it seems to be quite self contained:

def initiate_send(self):
    num_sent = 0
    num_sent = dispatcher.send(self, self.out_buffer[:512])
    self.out_buffer = self.out_buffer[num_sent:]

My question: Why is num_sent first set to 0, but then immediately set again to another value?

If i'd found this anywhere but in the python source code, I'd say that this line of code is superfluous. Is there any point to it, or is it dead code?

like image 722
Lauritz V. Thaulow Avatar asked Sep 10 '12 12:09

Lauritz V. Thaulow


1 Answers

That's dead code; it's a local variable, so it is not ever going to influence anything else.

If it had been a instance variable, then dispatcher.send could have read the value while executing, but it's completely superfluous here.

The asyncore module is relatively ancient, the line has been there since it was first committed to the python codebase in 1999.

like image 159
Martijn Pieters Avatar answered Sep 20 '22 14:09

Martijn Pieters