Discover instance variables
Steve Holden
steve at holdenweb.com
Mon Jul 16 14:18:48 EDT 2007
JonathanB wrote:
> Ok, I know there has to be a way to do this, but my google-fu fails me
> (once more). I have a class with instance variables (or should they be
> called attributes, I'm newish to programming and get really confused
> with OO sometimes), like the one in this example:
>
Instance variable are indeed attributes, but you are doing fine with the
language so don't worry about it.
> class Foo():
> self.a = "bar"
> self.z = "test"
> self.var = 2
>
That's unlikely to work, though: the code is in the context of the
class, not one of its methods, so unless you happen to be declaring a
class inside another class's method it's unlikely that there's going to
be a "self" around when those three lines execute.
What you probably want is something like what follows (I am running it
interactively so I know I am telling the truth: it keeps me honest :-).
You should get used to using the interpreter to check your hypotheses -
it would have told you you were making a mistake above as soon as you
tried to create a Foo.
>>> class Foo:
... a = "bar"
... z = "test"
... var = 2
...
You can check that Foo (the class) has these attributes:
>>> Foo.a
'bar'
> foo = Foo()
>
Now foo is an instance of the Foo class. Class attributes can also be
accessed through instances:
>>> foo = Foo()
>>> foo.var
2
>>>
Binding an instance attribute, however, doesn't change the class, so you
can use class attributes as a kind of "default" for instance.
>>> foo.a = "foo"
>>> foo.a
'foo'
>>> Foo.a
'bar'
>>>
> I want to print a list of the variables (strings, integers, etc) in
> Foo, without knowing their names. So that the user who needs to change
> a peice of information can view the variable list, enter the name of
> the variable they need to change and then make the change. This is
> what I'd like a sample run to look like:
>
> Attributes for foo:
> a = bar
> z = test
> var = 2
>
> Change: a
> New value: foobar
> Change made!
>
> I can handle the formatting and changing the variable itself, no
> problem, but how do I get a list of all the variables in a class
> instance? I almost put a list in there called vars and appended all
> the variables to it so I could iterate the list, but this sounds like
> something useful enough that someone has come up with a better way to
> do it than that. It almost looks like self.__dir__() is what I want,
> but that returns methods as well, correct? I only want variables, but
> I can't think of how to do a list comprehension that would remove the
> methods.
>
[name for name in dir(x) if not callable(name) and not
name.startswith("__")]
might come close - I presume you don't want __doc__ and the like.
regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------
More information about the Python-list
mailing list