Re: cymbalic reference?

Discussion in 'Python' started by Benjamin Kaplan, Jan 16, 2013.

  1. On Tue, Jan 15, 2013 at 8:56 PM, rh <> wrote:
    > I have this working and I am curious to know how others do same.
    >
    > class Abc(object):
    > def __init__(self):
    > pass
    > def good(self):
    > print "Abc good"
    > def better(self):
    > print "Abc better"
    >
    > urls = {'Abc':'http://example.com'}
    > strings = ['good', 'better']
    >
    > for s in urls:
    > o = eval("%s()" % s)
    > for string in strings:
    > eval("o.%s()" % string)
    >
    >
    > Yes, 'spose symbolic references is what these are....
    >
    > While I'm at it what magic could I use to print "the-class-I-am-in good"
    > instead of hard-coding "Abc good"? I tried __class_ and self.__class__
    >
    > --


    Rather than using eval, you can grab the class out of globals(), and
    then use getattr to get the methods.

    >>> for s in urls :

    .... o = globals()()
    .... for method in strings :
    .... getattr(o, method)()
    ....
    Abc good
    Abc better

    And for getting the class name, the class has a __name__ attribute. So
    you could use self.__class__.__name__.
    Benjamin Kaplan, Jan 16, 2013
    #1
    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. rh

    cymbalic reference?

    rh, Jan 16, 2013, in forum: Python
    Replies:
    0
    Views:
    126
  2. Chris Angelico

    Re: cymbalic reference?

    Chris Angelico, Jan 16, 2013, in forum: Python
    Replies:
    0
    Views:
    148
    Chris Angelico
    Jan 16, 2013
  3. Terry Reedy

    Re: cymbalic reference?

    Terry Reedy, Jan 16, 2013, in forum: Python
    Replies:
    0
    Views:
    126
    Terry Reedy
    Jan 16, 2013
  4. rh

    Re: cymbalic reference?

    rh, Jan 16, 2013, in forum: Python
    Replies:
    0
    Views:
    145
  5. rh

    Re: cymbalic reference?

    rh, Jan 16, 2013, in forum: Python
    Replies:
    0
    Views:
    146
Loading...

Share This Page