On 07/12/2018 02:13, Avi Gross wrote: > Alan says he had a challenge evaluating code (below) because the same > variable names were reused
It wasn't the reuse per se but the generic nature of the names combined with reuse. Reusing names like i,j,k for simple integer indices etc is not a problem. But when values of domain significance are given the same generic name (like item or file) then it gets confusing. Its very easy to miss a significant reassignment of the variable to a new value. > ... In languages without garbage collection, reusing > the same name, like "index" repeatedly might save some > small amount of space. Garbage collection only helps if the variable loses its assignment. If the variable retains its value it won't be garbage collected. So the same space saving applies. However it is very rare that Python is used in the kind of scenarios where that kind of space saving is significant (usually embedded controllers with tiny memory spaces). > if that is important, you can use "del var" to remove var. Exactly so. > But so much code I see in python does not only > reuse the same variable > names but in a confusing way. Then its probably badly written Python! > file = "some name" > file = open(file, "r") > file = some_wrapper(file) This is a good example of poor reuse since the objects are all totally different and only the second it truly a file. > mylist = [ ... ] > mylist = sorted(mylist) That's not really reusing it, it's just a convoluted way of modifying the value. (IN a purely functional view of the world it is reuse but only a functional purist would argue it that way I suspect) > for index in range(...): > stuff THis usage is possibly ok if range is used to generate indexes into some collection. Although probably iterating over the colection itself would be preferred. > for index in open(...) > more stuff But this is bad since index is not an index its a line, and possibly even more specifically a domain object representation which should get a domain related name. > for index, index2 in enumerate(...) > yet more stuff Again the index is OK here but index2 is very wrong since it holds the value not an index. But these issues are not Python issues they are general naming issues in all languages. It is good practice, regardless of language, to use problem domain names for variables not generic names or type related names. And also to not reuse variables if the name is not applicable to the new value. > I have often seen something like this done with methods, such as to emulate > decorator functionality It's quite common to decorate a method and assign the result to the same name but that is akin to the sorted list example above. The name is being augmented by the decorator not used for a different purpose. It is specifically being assigned to the same name to hide the original non augmented function object. > So is there a guide on when reuse is good > and when it just obfuscates? It's the same in Python as in any language. Use a name that makes sense in the domain. If that name is applicable in multiple places its ok to use the same name. If a name is non domain specific then it should only be used in very localised contexts - such as a loop body - or where it will be replaced by a domain specific variable immediately - such as a tuple from a database being used to instantiate a domain related object. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor