Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Grabbing a object from the current code block using a callable statement?

Reply
Thread Tools

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

 
 
ChaosKCW
Guest
Posts: n/a
 
      02-25-2006
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,

 
Reply With Quote
 
 
 
 
Larry Bates
Guest
Posts: n/a
 
      02-25-2006
ChaosKCW wrote:
> 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:

>>> class foo:

.... 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Making Non Callable Objects Callable exiquio Python 2 10-07-2008 06:02 PM
Callable class with block blondinet Ruby 13 01-15-2008 07:02 PM
Fo:Block can you check to see if a block contains any text by using the block id? morrell XML 1 10-10-2006 07:18 PM
grabbing current username via ADSI? Tom Petersen ASP General 3 01-18-2005 08:50 PM



Advertisments