[Python-bugs-list] [ python-Bugs-593154 ] Can't assign to __name__ or __bases__ of new class
noreply@sourceforge.net
noreply@sourceforge.net
Sun, 18 Aug 2002 15:57:05 -0700
Bugs item #593154, was opened at 2002-08-09 12:55
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=593154&group_id=5470
Category: Type/class unification
Group: Python 2.3
Status: Open
Resolution: None
Priority: 3
Submitted By: Guido van Rossum (gvanrossum)
Assigned to: Guido van Rossum (gvanrossum)
Summary: Can't assign to __name__ or __bases__ of new class
Initial Comment:
While it's rarely useful, there *are* situations where
assigning to __name__ or __bases__ of a class might
make sense. So maybe this should be allowed.
----------------------------------------------------------------------
>Comment By: Raymond Hettinger (rhettinger)
Date: 2002-08-18 17:57
Message:
Logged In: YES
user_id=80475
Closing bug 591135 and merging it into this one. Posting
from Joost Jacob:
"""
In article:
http://www.linuxjournal.com/article.php?sid=4540
Chuck Esterbrook mentions an elegant technique
called 'Mixin'.
Assigning to __bases__ is useful if you want to add
ancestors without changing the object's source code.
It does not work with new-style classes because the
.__bases__ attribute is now read-only.
This is also mentioned on
http://www.python.org/2.2.1/bugs.html
at the bottom but I could not find a bug-report here at
SF.
"""
----------------------------------------------------------------------
Comment By: Guido van Rossum (gvanrossum)
Date: 2002-08-09 15:21
Message:
Logged In: YES
user_id=6380
I should've mentioned that this is only a problem for
new-style classes. To see the difference, try "class
lcl(object):" instead of "class lcl:" in your example.
But perhaps you should consider using a metaclass if you
really want the class objects to have different names.
----------------------------------------------------------------------
Comment By: Gregory Smith (gregsmith)
Date: 2002-08-09 15:16
Message:
Logged In: YES
user_id=292741
e.g.
def mkclass(cname,something):
class lcl:
def __init__(self):
... yada...
def f(self, parm = something):
... yada ...
... yada...
lcl.__name__ = cname
return lcl
My understanding is that this function returns a different
class object (each cut from the same cloth, as it were)
each time it is called, but they all have the same name
by default.
If you are doing this (which is a cool thing to be able to do)
it certainly makes sense to give each class a different
name. I just tried this, however, and it seems to work.
__name__ got changed, and instances have the new name
in their default repr().
----------------------------------------------------------------------
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=593154&group_id=5470