Using eval, or something like it...

r0g aioe.org at technicalbloke.com
Thu Nov 20 18:54:18 EST 2008


Scott David Daniels wrote:
> r0g wrote:
>> John Machin wrote:
>>> .... You mention "variables of a class" but you then proceed to poke
>>> at an instance of the class....
>>> Check out setattr (and getattr) in the docs.
>> The former i.e. the variables of an instance of a class. Thanks :-)
> 
> Careful here.  Your wording seems to indicate you misunderstand the
> Python model.  The instance doesn't have variables (and your class built
> nothing that could be called an instance variable).  Think of the
> attributes of an instance (or a class) as "values attached to (or
> associated with) the instance."  If you don't you are setting yourself
> up to discover a pile of bugs that you don't understand.
> 
> --Scott David Daniels
> Scott.Daniels at Acm.Org


OK now I'm confused, let me explain how I see things at the moment and
you can correct me...

A class is like a template which combines a complex data type (made from
a combination of other data types) and the methods that operate on that
data type.

You generally don't work with classes directly but you make instances of
them, each instance has it's own internal state and methods, initially
these are the same as the templates but can be changed or overridden
without affecting the state of any other instances you might have.

While the perceived wisdom is that you should encapsulate all the
methods you need to modify your classes' state within the class itself
Python does (for better or worse) permit you to reach inside a class and
futz with it's state directly from outside.

The bits of an instance's state one might futz with (from within or
without) i.e. the primitives that make up the complex object the class
is a representation of, I think of as it's variables.

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...

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?

Also then, what _is_ an "instance variable" ?

Thanks,


Roger.

Q: How many pedants does it take to change a lightbulb?
A: Well actually you mean "replace" a lightbulb.
Q: Have you ever kissed a girl?



More information about the Python-list mailing list