[ python-Bugs-1019048 ] __metaclass__ in locals is ignored

SourceForge.net noreply at sourceforge.net
Sun Sep 5 22:10:17 CEST 2004


Bugs item #1019048, was opened at 2004-08-30 05:52
Message generated for change (Comment added) made by bcannon
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1019048&group_id=5470

Category: Python Interpreter Core
Group: None
>Status: Closed
>Resolution: Invalid
Priority: 5
Submitted By: Jeff Epler (jepler)
Assigned to: Nobody/Anonymous (nobody)
Summary: __metaclass__ in locals is ignored

Initial Comment:
The local namespace is not checked for a __metaclass__
when constructing a new class.  This may be consistent
with a strict reading of the language standard
[http://docs.python.org/ref/metaclasses.html#l2h-216]
but it's surprising.

I think that the lookup for __metaclass__ should
proceed in the same way as any other name lookup (after
it is not found in the namespace of the class-to-be),
which would include a lookup in the local namespace.

The attached file should print "metaclass called with
..." and "C constructed as None..." but it doesn't in
the "local case"

If this behavior is not a bug, would a doc patch
emphasizing the non-lookup in locals be accepted?

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

>Comment By: Brett Cannon (bcannon)
Date: 2004-09-05 13:10

Message:
Logged In: YES 
user_id=357491

It's not a bug.  If you wanted your little class to have a metaclass it 
should have been defined in a __metaclass__ attribute.  The whole global 
__metaclass__ thing in the global namespace is just a helper situation 
when you want all code to have the same metaclass, but also just to 
move old code over to new-style.  It is still better practice to list the 
metaclass explicitly in the class.

As for a patch about locals, I don't think it is needed since it is fairly 
explicit about what cases lead to a metaclass being used.  Always go 
with a strict reading of the docs.

I wouldn't mind seeing a patch that clarified the first bullet point about 
``dict[__metaclass__]`` and a line saying that specifying it as a class 
attribute  is the best practice, though.  But that should be a separate 
patch and not tied to this bug report which I am closing.

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

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1019048&group_id=5470


More information about the Python-bugs-list mailing list