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

Discussion in 'Python' started by ChaosKCW, Feb 25, 2006.

  1. ChaosKCW

    ChaosKCW Guest

    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,
    ChaosKCW, Feb 25, 2006
    #1
    1. Advertising

  2. ChaosKCW

    Larry Bates Guest

    Re: Grabbing a object from the current code block using a callablestatement?

    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
    Larry Bates, Feb 25, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. morrell
    Replies:
    1
    Views:
    936
    roy axenov
    Oct 10, 2006
  2. exiquio
    Replies:
    2
    Views:
    523
    exiquio
    Oct 7, 2008
  3. Tom Petersen

    grabbing current username via ADSI?

    Tom Petersen, Jan 18, 2005, in forum: ASP General
    Replies:
    3
    Views:
    109
    Jeff Cochran
    Jan 18, 2005
  4. blondinet

    Callable class with block

    blondinet, Jan 12, 2008, in forum: Ruby
    Replies:
    13
    Views:
    209
    Robert Dober
    Jan 15, 2008
  5. Ulrich Eckhardt
    Replies:
    6
    Views:
    101
    Peter Otten
    Jul 12, 2013
Loading...

Share This Page