[Python-bugs-list] [ python-Bugs-593154 ] Can't assign to __name__ or __bases__ of new class

noreply@sourceforge.net noreply@sourceforge.net
Tue, 26 Nov 2002 07:09:19 -0800


Bugs item #593154, was opened at 2002-08-09 18: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: Closed
>Resolution: Fixed
Priority: 3
Submitted By: Guido van Rossum (gvanrossum)
Assigned to: Michael Hudson (mwh)
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: Michael Hudson (mwh)
Date: 2002-11-26 15:09

Message:
Logged In: YES 
user_id=6656

Yup, I think we can call this one dead.  My patch was
#635933, for reference.

----------------------------------------------------------------------

Comment By: Neal Norwitz (nnorwitz)
Date: 2002-11-26 14:57

Message:
Logged In: YES 
user_id=33168

Michael, can this be closed now that your patch has been
checked in?

----------------------------------------------------------------------

Comment By: Raymond Hettinger (rhettinger)
Date: 2002-08-18 23: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 21: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 21: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