Feature request: String-inferred names

Brad Harms fearsomedragonfly at gmail.com
Fri Dec 4 00:12:39 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>'

...I'm not sure I follow your logic.

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. However, to do so manually
in this manner completely disregards the fundamentals of object-oriented
programming, not to mention the basic guiding principles of nearly all
Python code in existence. It totally breaks inheritance and
polymorphism. Maybe I'm missing something, but I can't see how that line
of thought helps anything.




More information about the Python-list mailing list