Style question: metaclass self vs cls?

Discussion in 'Python' started by Steven D'Aprano, Jul 16, 2012.

  1. 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
    Steven D'Aprano, Jul 16, 2012
    #1
    1. Advertising

  2. Steven D'Aprano

    Terry Reedy Guest

    On 7/16/2012 11:29 AM, Steven D'Aprano wrote:
    > 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.


    I have never seriously written a metaclass, but as a reader I would
    prefer 'cls'.

    > On the third-hand, it may be confusing that the argument is called "cls"
    > but not decorated with classdecorator.


    To me, that reinforces 'looking as a metaclass'.

    An @classmethod in a class is a class method specific to the particular
    class. A method in a metaclass is a method common to all classes of the
    metaclass. They could be written differently, yet calling the first
    param 'cls' either way seems reasonable.

    > 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?


    Not too many people write real metaclasses. Python lets you chose. Have
    you looked at the C code of type? (Not that you are bound by it.)

    --
    Terry Jan Reedy
    Terry Reedy, Jul 16, 2012
    #2
    1. Advertising

  3. Steven D'Aprano

    alex23 Guest

    On Jul 17, 1:29 am, Steven D'Aprano <steve
    > wrote:
    > Here's a style question for you: in a metaclass, what should I call the
    > instance parameter of methods, "cls" or "self"?


    Maybe portmanteu it as "clasself"? :)
    alex23, Jul 17, 2012
    #3
  4. The standard is to use `cls`. In the __new__ method you can use `mcl` or `meta`.
    Michele Simionato, Jul 17, 2012
    #4
  5. Steven D'Aprano

    Ian Kelly Guest

    On Tue, Jul 17, 2012 at 6:23 AM, Michele Simionato
    <> wrote:
    > The standard is to use `cls`. In the __new__ method you can use `mcl` or `meta`.


    I've also seen `mcs` a fair amount.
    Ian Kelly, Jul 17, 2012
    #5
  6. Steven D'Aprano

    Ian Kelly Guest

    On Tue, Jul 17, 2012 at 12:10 AM, alex23 <> wrote:
    > On Jul 17, 1:29 am, Steven D'Aprano <steve
    > > wrote:
    >> Here's a style question for you: in a metaclass, what should I call the
    >> instance parameter of methods, "cls" or "self"?

    >
    > Maybe portmanteu it as "clasself"? :)


    What is this, 1st edition D&D? Elf is a race, not a class.
    Ian Kelly, Jul 17, 2012
    #6
  7. On Tue, 17 Jul 2012 05:23:22 -0700, Michele Simionato wrote:

    > The standard is to use `cls`. In the __new__ method you can use `mcl` or
    > `meta`.


    Thanks to everyone who answered.

    I think I will stick with "meta" and "cls".


    --
    Steven
    Steven D'Aprano, Jul 18, 2012
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David MacQuigg

    Doc error on super(cls,self)

    David MacQuigg, Jun 11, 2004, in forum: Python
    Replies:
    2
    Views:
    281
    David MacQuigg
    Jun 12, 2004
  2. ironfroggy
    Replies:
    16
    Views:
    423
    Michele Simionato
    Jun 3, 2005
  3. james_027

    cls & self

    james_027, Jul 26, 2007, in forum: Python
    Replies:
    3
    Views:
    307
    Chris Mellon
    Jul 27, 2007
  4. J Peyret
    Replies:
    10
    Views:
    412
    Steven D'Aprano
    Mar 19, 2011
  5. Steven D'Aprano

    Metaclass of a metaclass

    Steven D'Aprano, Jun 5, 2012, in forum: Python
    Replies:
    1
    Views:
    293
Loading...

Share This Page