how to get names of attributes
Chris Angelico
rosuav at gmail.com
Wed Dec 30 08:45:05 EST 2015
On Thu, Dec 31, 2015 at 12:31 AM, Charles T. Smith
<cts.private.yahoo at gmail.com> wrote:
> Okay, thank you. I'm trying to understand your program.
>
> Unfortunately, I haven't gotten the same output you had, using python 2.6
> or 2.7. Maybe I haven't been able to restore the indentation correctly
> after having been filtered through pan(1).
>
> I wonder what the difference is between vars() and items()
What I sent you was a log of interactive Python, so there are prompts
and continuation prompts. Here's a script version of the same thing
(running under CPython 2.7):
class X: pass
class Y(X): pass
class Z(Y): pass
X.x=1
Y.y=2
Z.z=3
inst=Z()
inst.i=4
def class_vars(old_style_class):
v = {}
for cls in old_style_class.__bases__:
v.update(class_vars(cls))
v.update(vars(old_style_class))
return v
def all_vars(old_style_inst):
v = class_vars(old_style_inst.__class__)
v.update(vars(old_style_inst))
return v
print(all_vars(inst))
# {'i': 4, '__module__': '__main__', 'y': 2, 'x': 1, 'z': 3, '__doc__': None}
Does that work better?
ChrisA
More information about the Python-list
mailing list