Can't do a multiline assignment!

Michael Torrie torriem at gmail.com
Thu Apr 17 19:24:54 CEST 2008


s0suk3 at gmail.com wrote:
> <code snipped>
> There! That's the whole code. I guess the way you suggest is simpler
> and a bit more intuitive, but I was figuring that the way I suggested
> it is more stylish.

Umm, doesn't defining all those members in the class lead to class
variables, not instance variables?  I believe the recommended way of
making it clear what instance variables to expect is to initialize them
all in __init__.  Currently in your implementation, each instance of
your class is going to share the same variables for all those fields you
defined, which probably isn't what you want.

consider:

class myclass(object):
    classvar1=None
    classvar2=None

    def __init__(self,test):
        self.instancevar1=test

>>> a=myclass(3)
>>> b=myclass(6)
>>> a.classvar1=9
>>> a.classvar1
9
>>> b.classvar1
9
>>> a.instancevar1
3
>>> b.instancevar1
6

Also, your idea of checking the length of the headers to reduce the
number of string comparisons is a great case of premature optimization.
 First it does not clarify the code, making it harder to follow.
Second, since web servers are I/O bound, it likely does nothing to
improve speed.

So my recommendation is to use a bunch of self.HEADER_NAME=None
declarations in __init__().  This is the expected way of doing it and
all python programmers who are looking at your code will immediately
recognize that they are instance variables.



More information about the Python-list mailing list