Using eval, or something like it...

George Sakkis george.sakkis at gmail.com
Thu Nov 20 20:28:44 EST 2008


On Nov 20, 6:54 pm, r0g <aioe.... at technicalbloke.com> wrote:

> It would seem from this setattr function that the proper term for these
> is 'attributes'. That for many years I have considered pretty much any
> named thing that may vary a 'variable' might be at the root of the
> problem here as it's a very un-specific term...

Exactly, refrain from using the term "variable", or "value" for that
matter, in Python context as they are notorious for causing more
trouble than they deserve..

> So I gather you are saying that the fragments of state within a class
> are so distinct from ordinary common or garden variables that it is
> incorrect to think of them, or describe them, as variables, much like
> quarks should not really be regarded as distinct particles, and they
> should only be thought of and described as 'attributes' to avoid confusion?
>
> Is this correct enough for me to avoid the aforementioned bug pile?

No, a class can have attributes just like a instance can, and you can
use setattr() to set an attribute for a class just like you do it for
instances:

class Foo():
  bar = 1
  gum = 2

>>> setattr(Foo, 'bar', 3)
>>> Foo.bar
3

George



More information about the Python-list mailing list