Chris's Wiki :: blog/python/MetaclassCallableIssues Commentshttps://utcc.utoronto.ca/~cks/space/blog/python/MetaclassCallableIssues?atomcommentsDWiki2015-03-07T23:10:38ZRecent comments in Chris's Wiki :: blog/python/MetaclassCallableIssues.By Chris Siebenmann on /blog/python/MetaclassCallableIssuestag:CSpace:blog/python/MetaclassCallableIssues:8259f9e6c72c2f23b0d51f55b30dd372031a836cChris Siebenmann<div class="wikitext"><p>Wow, you're right, and I hadn't realized that before. Just invoking a
metaclass as 'T()' winds up calling <code>T.__new__</code>, not <code>T.__call__</code>.
Which kind of makes sense, since metaclasses don't have conventional
instances, but it's still wacky.</p>
<p>Note that this actually happens for <em>all</em> classes, not just metaclasses
(which is what we should expect, since <code>type</code> is the type of most regular
classes as well). Class <code>__call__</code> is only invoked when instances are
called, not when the class itself is. I don't know why I got confused
about that, but I think I need to write another entry about it.</p>
</div>2015-03-07T23:10:38ZBy NYKevin on /blog/python/MetaclassCallableIssuestag:CSpace:blog/python/MetaclassCallableIssues:5aadbd111ee75c361453be527c9af133046aa37bNYKevinhttp://principiaconcordia.blogspot.com<div class="wikitext"><p>>PS: Note that Python is actually inconsistent here between real metaclass classes and other callables, since a metaclass that is a class will have its __new__ invoked, not its __call__, even if it has the latter and thus is callable in general.</p>
<p>This is metaclass confusion at its finest. Remember: to call some object x, Python always directly invokes type(x).__call__(x, *args, **kwargs), rather than doing x.__call__(*args, **kwargs) and relying on the usual method resolution machinery. In this case, x is a metaclass. type(x) is (probably) type, unless your metaclasses have metaclasses of their own (which I should <em>hope</em> they don't, for the sake of readability!), and type.__call__ just calls into __new__() and __init__() as normal.</p>
</div>2015-03-07T22:53:12ZBy Peter Donis on /blog/python/MetaclassCallableIssuestag:CSpace:blog/python/MetaclassCallableIssues:6118fe6041bd94b2fa88e49da5814434f8817451Peter Donishttp://blog.peterdonis.com<div class="wikitext"><p>I actually don't even buy the "convenience" argument for letting any callable be a metaclass. Class decorators provide the same functionality and are a lot less confusing (in particular, it's a lot clearer that the decorator only operates on the class it decorates, not on subclasses).</p>
</div>2015-02-18T17:13:31Z