This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Can't assign to __name__ or __bases__ of new class
Type: Stage:
Components: Interpreter Core Versions: Python 2.3
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: mwh Nosy List: gregsmith, gvanrossum, mwh, nnorwitz, rhettinger
Priority: low Keywords:

Created on 2002-08-09 17:55 by gvanrossum, last changed 2022-04-10 16:05 by admin. This issue is now closed.

Messages (6)
msg11890 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2002-08-09 17:55
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.
msg11891 - (view) Author: Gregory Smith (gregsmith) Date: 2002-08-09 20:16
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(). 

msg11892 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2002-08-09 20:21
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.
msg11893 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2002-08-18 22:57
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. 
"""
msg11894 - (view) Author: Neal Norwitz (nnorwitz) * (Python committer) Date: 2002-11-26 14:57
Logged In: YES 
user_id=33168

Michael, can this be closed now that your patch has been
checked in?
msg11895 - (view) Author: Michael Hudson (mwh) (Python committer) Date: 2002-11-26 15:09
Logged In: YES 
user_id=6656

Yup, I think we can call this one dead.  My patch was
#635933, for reference.
History
Date User Action Args
2022-04-10 16:05:34adminsetgithub: 37011
2002-08-09 17:55:30gvanrossumcreate