Grabbing a object from the current code block using a callable statement?

C

ChaosKCW

Hi

Is it possible to grab get an object returned from a string and a
callable ? e.g

I pass in a key value pair:

def somemethod(adict = {'new name for object': '<code to reutrn
obejct>'}):

object = .....

for key, value in adict.items():
if callable(value):
somedict[key] = value()
else:
somedict[key] = value

such that

somedict = {'new name for object': object}

??

Thanks,
 
L

Larry Bates

ChaosKCW said:
Hi

Is it possible to grab get an object returned from a string and a
callable ? e.g

I pass in a key value pair:

def somemethod(adict = {'new name for object': '<code to reutrn
obejct>'}):

object = .....

for key, value in adict.items():
if callable(value):
somedict[key] = value()
else:
somedict[key] = value

such that

somedict = {'new name for object': object}

??

Thanks,

If you are asking if an object instance can be stored in a dictionary
the answer is yes.

example:
.... def method(self):
.... print "in foo.method"
.... return
....
adict={'fooclass': foo()}
adict['fooclass'].method() in foo.method

Instances of classes are just like any other data you might put into
a list or a dictionary. I hope that is what you were asking.

-Larry Bates
 

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

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top