Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: cymbalic reference?

Thread Tools

Re: cymbalic reference?

Terry Reedy
Posts: n/a
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':''}
> 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()[s]()
for string in strings:

has same output. eval is seldom needed.
Of course, if you more sensibly use the class or an instance as key
urls = {Abc: ''}
urls = {Abc(): ''}
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

Reply With Quote

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
Re: cymbalic reference? rh Python 0 01-16-2013 09:06 PM
Re: cymbalic reference? rh Python 0 01-16-2013 08:52 PM
Re: cymbalic reference? Benjamin Kaplan Python 0 01-16-2013 05:08 AM
Re: cymbalic reference? Chris Angelico Python 0 01-16-2013 05:03 AM
cymbalic reference? rh Python 0 01-16-2013 04:56 AM