Confounded by Python objects

satoru torainLight at gmail.com
Thu Jul 24 20:42:54 EDT 2008


On Jul 24, 6:10 pm, "boblat... at googlemail.com"
<boblat... at googlemail.com> wrote:
> On Jul 24, 11:59 am, Fredrik Lundh <fred... at pythonware.com> wrote:
>
> > tip: if you're not 100% sure why you would want to put an attribute
> > on the class level, don't do it.
>
> The reason I did it was sort of C++ish (that's where I come from): I
> somehow wanted a list of attributes on the class level. More for
> readibility than anything elase, really.
>
> > hope this helps!
>
> Yup, did the trick. Thanks!
> robert

yes, i thought your code is kind of static, so it didn't work for a
dynamic language like python.
in python, you don't have to say "static" to make an variable a class
variable, so the "name" and "sample" you kind of "declared" is indeed
class variables.
you may wonder why then the two instaces of "Channel" has different
names, that's because you assign to name in "__init__" and make it an
instance variable that shared the name "name" with a class variable.
As to "sample", it never get assigned to and when you say "append" the
class variable is changed in place.
hope my explaination helps.



More information about the Python-list mailing list