Re: cymbalic reference?

Discussion in 'Python' started by Terry Reedy, Jan 16, 2013.

  1. Terry Reedy

    Terry Reedy Guest

    On 1/15/2013 11: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)


    for s in urls:
    o = globals()()
    for string in strings:
    getattr(o,string)()

    has same output. eval is seldom needed.
    Of course, if you more sensibly use the class or an instance as key
    urls = {Abc: 'http://example.com'}
    urls = {Abc(): 'http://example.com'}
    instead of the name, then o = s() or o = s directly.

    > 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__


    self.__class__.__name__ or type(self).__name__)

    --
    Terry Jan Reedy
    Terry Reedy, 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:
    135
  2. Chris Angelico

    Re: cymbalic reference?

    Chris Angelico, Jan 16, 2013, in forum: Python
    Replies:
    0
    Views:
    153
    Chris Angelico
    Jan 16, 2013
  3. Benjamin Kaplan

    Re: cymbalic reference?

    Benjamin Kaplan, Jan 16, 2013, in forum: Python
    Replies:
    0
    Views:
    144
    Benjamin Kaplan
    Jan 16, 2013
  4. rh

    Re: cymbalic reference?

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

    Re: cymbalic reference?

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

Share This Page