Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

Re: cymbalic reference?

 
 
Terry Reedy
Guest
Posts: n/a
 
      01-16-2013
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()[s]()
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

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



Advertisments