Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #25444
| Path | csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!newsfeed.eweka.nl!eweka.nl!feeder3.eweka.nl!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python-python-list@m.gmane.org> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'argument': 0.04; 'class,': 0.07; 'parameter': 0.07; 'subject:question': 0.08; 'python': 0.09; '*args):': 0.09; 'methods,': 0.09; 'other,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'terry': 0.09; 'looked': 0.10; 'def': 0.10; 'slightly': 0.15; '@classmethod': 0.16; 'message-id:@dough.gmane.org': 0.16; 'metaclass': 0.16; 'metaclasses': 0.16; 'metaclasses.': 0.16; 'param': 0.16; 'reasonable.': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'wrote:': 0.17; 'instance': 0.17; 'jan': 0.18; '(not': 0.20; 'written': 0.20; 'lets': 0.22; 'class.': 0.23; 'this:': 0.23; 'seems': 0.23; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'common': 0.26; 'am,': 0.27; 'right.': 0.27; 'question': 0.27; 'skip:e 30': 0.27; 'header:X-Complaints-To:1': 0.28; "d'aprano": 0.29; 'steven': 0.29; 'skip:_ 10': 0.29; 'class': 0.29; "i'm": 0.29; 'classes': 0.30; 'code': 0.31; 'towards': 0.32; 'could': 0.32; 'to:addr:python-list': 0.33; 'self': 0.34; 'subject:?': 0.35; 'received:org': 0.36; 'but': 0.36; 'method': 0.36; 'should': 0.36; 'too': 0.36; 'quite': 0.37; 'rather': 0.37; 'subject:: ': 0.38; 'sure': 0.38; 'to:addr:python.org': 0.39; 'called': 0.39; 'header:Received:5': 0.40; 'real': 0.61; 'first': 0.61; 'more': 0.63; 'obvious': 0.71; 'you:': 0.75; 'confusing': 0.84; 'received:fios.verizon.net': 0.84; 'subject:self': 0.84; 'hand,': 0.97 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | Terry Reedy <tjreedy@udel.edu> |
| Subject | Re: Style question: metaclass self vs cls? |
| Date | Mon, 16 Jul 2012 18:21:06 -0400 |
| References | <50043377$0$29978$c3e8da3$5496439d@news.astraweb.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=UTF-8; format=flowed |
| Content-Transfer-Encoding | 7bit |
| X-Gmane-NNTP-Posting-Host | pool-173-75-251-66.phlapa.fios.verizon.net |
| User-Agent | Mozilla/5.0 (Windows NT 6.1; WOW64; rv:13.0) Gecko/20120614 Thunderbird/13.0.1 |
| In-Reply-To | <50043377$0$29978$c3e8da3$5496439d@news.astraweb.com> |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.12 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.2192.1342477286.4697.python-list@python.org> (permalink) |
| Lines | 44 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1342477286 news.xs4all.nl 6986 [2001:888:2000:d::a6]:39831 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:25444 |
Show key headers only | View raw
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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
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
csiph-web