oop in python

jmdeschamps at gmail.com jmdeschamps at gmail.com
Fri Dec 30 14:19:40 EST 2005


Larry Bates wrote:
> novice wrote:
> > hello over there!
> > I have the following question:
> > Suppose I created a class:           class Point:
> >                                                           pass
> > then instanciated an instance:      new = Point()
> > So now how to get the instance new as a string: like ' new '   ; Is
> > there any built in function or method
> >  for eg:
> >
> > class Point:
> >        def _func_that_we_want_(self):
> >           return .......
> > new._func_that_we_want_()  --->  ' new '
> >
>
> Just a wild guess, but I think he wants to refer to it by a
> name.  The best way to do this is to put the class instance
> in a dictionary and make the key  'new'
>
> instancedict={}
> instancedict['new']=Point()
>
> then you can call methods by:
>
> instancedict['new'].somemethod()
>
> -Larry Bates

Maybe it's more in the way of the builtin function *eval*...
example (you decide if this is what you mean ;-)  )
>>> class toto(object):
... 	def max(self):
... 		return 12
...
>>> t=toto()
>>> eval("t.max")
<bound method toto.max of <__main__.toto object at 0x00D4BB90>>
>>> eval("t.max()")
12




More information about the Python-list mailing list