Style question: metaclass self vs cls?
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Mon Jul 16 11:29:59 EDT 2012
Here's a style question for you: in a metaclass, what should I call the
instance parameter of methods, "cls" or "self"?
class ExampleMeta(type):
def method(self, *args): ...
I'm not quite sure if that feels right. On the one hand, self is the
ExampleMeta instance alright... but on the other, self is actually a
class, so I feel I want to call it "cls" rather than "self", which makes
it more obvious that you're looking at a metaclass.
On the third-hand, it may be confusing that the argument is called "cls"
but not decorated with classdecorator.
I'm very slightly leaning towards writing metaclasses like this:
class ExampleMeta(type):
def __new__(meta, *args): ...
def method(cls, *args): ...
class Example(metaclass=ExampleMeta):
def another_method(self): ...
What do others do?
--
Steven
More information about the Python-list
mailing list