object.*

D

DeadWisdom

I think there should be an ability in python to write something like
"object.*" which should return a dictionary object containing all the
members and their values for the given object. Maybe ".*" could be
treated as an operator. ::shrug::

Perhaps the functionality already exists in some other syntax; if so
please splain.
 
D

Diez B. Roggisch

DeadWisdom said:
I think there should be an ability in python to write something like
"object.*" which should return a dictionary object containing all the
members and their values for the given object. Maybe ".*" could be
treated as an operator. ::shrug::

Perhaps the functionality already exists in some other syntax; if so
please splain.


dict([(n, getattr(object, n)) for n in dir(object)])

Not really worth an operator :)
 
T

Tim Leslie

An approximation of this functionality can be obtained using the built
in dir() command. A quick example follows:.... def __init__(self):
.... self.a = 1
.... self.b = 2
.... def f(self):
.... return self.a + self.b
....
c = C()
dir(c) ['__doc__', '__init__', '__module__', 'a', 'b', 'f']
["c." + x for x in dir(c)] ['c.__doc__', 'c.__init__', 'c.__module__', 'c.a', 'c.b', 'c.f']
map(eval, ["c." + x for x in dir(c)])
[None, <bound method C.__init__ of <__main__.C instance at
0x401ec50c>>, '__main__', 1, 2, <bound method C.f of <__main__.C
instance at 0x401ec50c>>]

Cheers,

Tim
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,204
Messages
2,571,063
Members
47,670
Latest member
micheljon

Latest Threads

Top