Feature request: String-inferred names

Bradley K. Harms themusicguy123 at gmail.com
Fri Dec 4 01:35:28 EST 2009


On Tue, 2009-12-01 at 14:38 +0000, Steven D'Aprano wrote:
> On Mon, 30 Nov 2009 18:55:46 -0800, The Music Guy wrote:
> 
> > Lie Ryan, I think I see what you're saying about using __dict__ to add
> > members to a class, but it's not quite the same. __dict__ is only for
> > attributes, NOT properties, methods, etc. which all come from the class
> > of an object rather than the object's __dict__. 
> 
> Almost but not quite.
> 
> It's just special double-underscore methods like __init__ __add__ etc 
> that have to be in the class rather than the instance. (To be precise, 
> you can add such a method to the instance, but it won't be called 
> automatically.) Likewise staticmethods and classmethods won't work 
> correctly unless they are in the class. But ordinary methods work fine: 
> the only tricky bit is creating them in the first place.
> 
> >>> class K(object):
> ...     pass
> ...
> >>> k = K()
> >>> import types
> >>> k.method = types.MethodType(lambda self: "I am %s" % self, k)
> >>> k.method()
> 'I am <__main__.K object at 0xb7cc7d4c>'

> Yes, you can create an instancemethod out of a function and assign it to
> an instance after (or during) its instantiation, which is what Python's
> class/instance model provides automatically. [...]

...Hmm, let me clarify that statement as I think it could be misunderstood:

When I say "which is what [Python] does automatically", I mean that Python
automatically creates a BOUND instancemethod object from the UNBOUND
instancemethod whenever you try to access the unbound instancemethod as an
attribute of the instance _at the moment that you try to access it_. I did
NOT mean to imply that the bound instancemethod objects are created and
assigned to the instance at the time of its instantiation (because Python
obviously doesn't do that).

Sorry for the rapid-fire posting. 






More information about the Python-list mailing list