Can't do a multiline assignment!

s0suk3 at gmail.com s0suk3 at gmail.com
Thu Apr 17 13:46:22 EDT 2008


On Apr 17, 12:24 pm, Michael Torrie <torr... at gmail.com> wrote:
> s0s... 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.

You didn't really write that at the Python's interpreter, did you?
It's wrong. The way that would really go at the interpreter is like
this:

>>> 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
>>> # Nothing was output in this line because b.classvar1 is still None
...
>>> a.instancevar1
3
>>> b.instancevar1
6

classvar1 and classvar2 might be class variables, but in they don't
work as they would in C++ or Java (like the ones you declare with the
'static' modified).



More information about the Python-list mailing list