Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Re: cymbalic reference? (http://www.velocityreviews.com/forums/t956557-re-cymbalic-reference.html)

Benjamin Kaplan 01-16-2013 05:08 AM

Re: cymbalic reference?
 
On Tue, Jan 15, 2013 at 8:56 PM, rh <richard_hubbe11@lavabit.com> 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__.


All times are GMT. The time now is 08:21 PM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57