Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!news-out1.kabelfoon.nl!newsfeed.kabelfoon.nl!xindi.nntp.kabelfoon.nl!feed.xsnews.nl!border-2.ams.xsnews.nl!xlned.com!feeder7.xlned.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '__name__': 0.07; 'key.': 0.07; 'attribute.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'string)': 0.09; 'def': 0.10; 'benjamin': 0.16; 'globals(),': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'storing': 0.16; 'url:example': 0.16; 'string': 0.17; 'wrote:': 0.17; 'jan': 0.18; '>>>': 0.18; 'symbolic': 0.22; '15,': 0.23; 'class.': 0.23; 'needed.': 0.23; 'references': 0.23; 'idea': 0.24; 'pass': 0.25; 'tried': 0.25; 'header:User-Agent:1': 0.26; 'i.e.': 0.27; 'instead.': 0.27; 'correct': 0.28; 'header:X-Complaints-To:1': 0.28; 'methods.': 0.29; 'skip:_ 10': 0.29; 'class': 0.29; "i'm": 0.29; 'maybe': 0.29; 'could': 0.32; 'print': 0.32; 'getting': 0.33; 'curious': 0.33; 'to:addr:python-list': 0.33; 'along': 0.35; 'same.': 0.35; 'pm,': 0.35; 'subject:?': 0.35; 'received:org': 0.36; 'but': 0.36; 'method': 0.36; 'test': 0.36; 'charset:us-ascii': 0.36; 'uses': 0.37; 'rather': 0.37; 'subject:: ': 0.38; 'object': 0.38; 'sure': 0.38; 'instead': 0.39; 'to:addr:python.org': 0.39; 'skip:" 10': 0.40; 'header:Received:5': 0.40; 'your': 0.60; 'grab': 0.64; 'deeply': 0.66; '2013': 0.84; 'received:sd.cox.net': 0.84; 'abc': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: rh Subject: Re: cymbalic reference? Date: Wed, 16 Jan 2013 13:06:32 -0800 References: <20130115205604.178fa30eff8ae7155174d900@lavabit.com> Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: ip68-227-87-145.sb.sd.cox.net User-Agent: dsodnetnin X-Mailer: EZnn0.37p X-Newsreader: EZnn0.37p X-Gmane-NNTP-Posting-Host: EZnn0.37p Original-Received: from slem by 1.1 with local X-No-Archive: yes Archive: no X-Archive: expiry=11 X-Archive: encrypt X-Operating-System: Barebones_6.1 X-Gmane-NNTP-Posting-Host: 192.168.1.1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 74 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1358370409 news.xs4all.nl 6841 [2001:888:2000:d::a6]:33615 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:36916 On Tue, 15 Jan 2013 21:08:02 -0800 Benjamin Kaplan wrote: > 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()[s]() > ... 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__. My final product uses your suggestions along with one from the other post. I like the idea of storing the class name as the key. Then no call to globals() is needed. But I still have to test how that object behaves when it's a key. i.e. Is it deeply bound? Shallow? Tight? Loose? Not sure which is the correct term! So maybe I want {Abc:'http://example.com'} and o = s() instead. And a small change to the class. class Abc(object): def __init__(self): self.me = self.__class__.__name__ def good(self): print self.me, "good" def better(self): print self.me, "better" urls = {Abc():'http://example.com'} strings = ['good', 'better'] for s in urls: o = s for method in strings: getattr(o, method)() --