Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #25422 > unrolled thread
| Started by | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| First post | 2012-07-16 15:29 +0000 |
| Last post | 2012-07-18 02:50 +0000 |
| Articles | 7 — 5 participants |
Back to article view | Back to comp.lang.python
Style question: metaclass self vs cls? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-07-16 15:29 +0000
Re: Style question: metaclass self vs cls? Terry Reedy <tjreedy@udel.edu> - 2012-07-16 18:21 -0400
Re: Style question: metaclass self vs cls? alex23 <wuwei23@gmail.com> - 2012-07-16 23:10 -0700
Re: Style question: metaclass self vs cls? Ian Kelly <ian.g.kelly@gmail.com> - 2012-07-17 10:35 -0600
Re: Style question: metaclass self vs cls? Michele Simionato <michele.simionato@gmail.com> - 2012-07-17 05:23 -0700
Re: Style question: metaclass self vs cls? Ian Kelly <ian.g.kelly@gmail.com> - 2012-07-17 10:34 -0600
Re: Style question: metaclass self vs cls? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-07-18 02:50 +0000
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2012-07-16 15:29 +0000 |
| Subject | Style question: metaclass self vs cls? |
| Message-ID | <50043377$0$29978$c3e8da3$5496439d@news.astraweb.com> |
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
[toc] | [next] | [standalone]
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Date | 2012-07-16 18:21 -0400 |
| Message-ID | <mailman.2192.1342477286.4697.python-list@python.org> |
| In reply to | #25422 |
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
[toc] | [prev] | [next] | [standalone]
| From | alex23 <wuwei23@gmail.com> |
|---|---|
| Date | 2012-07-16 23:10 -0700 |
| Message-ID | <8f38a814-3c6c-42d3-8d7b-d7d8ae33d06e@x6g2000pbh.googlegroups.com> |
| In reply to | #25422 |
On Jul 17, 1:29 am, Steven D'Aprano <steve +comp.lang.pyt...@pearwood.info> 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"? :)
[toc] | [prev] | [next] | [standalone]
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Date | 2012-07-17 10:35 -0600 |
| Message-ID | <mailman.2227.1342542933.4697.python-list@python.org> |
| In reply to | #25464 |
On Tue, Jul 17, 2012 at 12:10 AM, alex23 <wuwei23@gmail.com> wrote: > On Jul 17, 1:29 am, Steven D'Aprano <steve > +comp.lang.pyt...@pearwood.info> 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.
[toc] | [prev] | [next] | [standalone]
| From | Michele Simionato <michele.simionato@gmail.com> |
|---|---|
| Date | 2012-07-17 05:23 -0700 |
| Message-ID | <7566b8bc-929e-4f70-8e44-f0bda232ef85@googlegroups.com> |
| In reply to | #25422 |
The standard is to use `cls`. In the __new__ method you can use `mcl` or `meta`.
[toc] | [prev] | [next] | [standalone]
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Date | 2012-07-17 10:34 -0600 |
| Message-ID | <mailman.2226.1342542877.4697.python-list@python.org> |
| In reply to | #25492 |
On Tue, Jul 17, 2012 at 6:23 AM, Michele Simionato <michele.simionato@gmail.com> 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.
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2012-07-18 02:50 +0000 |
| Message-ID | <50062471$0$1542$c3e8da3$76491128@news.astraweb.com> |
| In reply to | #25492 |
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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web