Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'example:': 0.03; 'subject:Python': 0.05; 'python': 0.08; 'arguments,': 0.09; 'foo': 0.09; 'imo.': 0.09; 'ma,': 0.09; 'rule.': 0.09; 'url:faq': 0.09; 'am,': 0.12; 'def': 0.13; '(besides': 0.16; '3:51': 0.16; 'confusing.': 0.16; 'foo(object):': 0.16; 'renaming': 0.16; 'simplest': 0.16; 'subject:classes': 0.16; 'cc:addr:python-list': 0.16; 'url:blog': 0.17; 'wrote:': 0.18; '>>>': 0.18; 'slightly': 0.19; 'seems': 0.20; 'cheers,': 0.20; 'cc:no real name:2**0': 0.21; 'header:In-Reply-To:1': 0.22; 'referring': 0.23; 'way?': 0.23; 'defined': 0.24; 'cc:2**0': 0.26; 'classes': 0.26; 'function': 0.27; 'pass': 0.29; 'message-id:@mail.gmail.com': 0.29; 'explicit': 0.29; 'class': 0.29; 'example': 0.29; 'problem': 0.29; 'skip:b 20': 0.29; 'cc:addr:python.org': 0.29; '22,': 0.30; 'url:2008': 0.30; 'chris': 0.30; 'subject:?': 0.31; 'thu,': 0.32; "i've": 0.32; 'break': 0.32; "isn't": 0.33; 'there': 0.33; 'mostly': 0.34; 'steven': 0.34; 'done': 0.34; 'rather': 0.34; 'url:python': 0.35; 'received:209.85.214': 0.36; 'but': 0.37; 'received:google.com': 0.37; 'another': 0.37; 'skip:_ 10': 0.38; 'received:209.85': 0.38; 'some': 0.38; 'initially': 0.39; 'url:org': 0.39; 'received:209': 0.39; 'cannot': 0.39; 'missing': 0.40; 'change': 0.40; 'unique': 0.61; "you'll": 0.61; 'url:blogspot': 0.61; 'body': 0.61; 'your': 0.61; 'here': 0.64; 'account': 0.66; 'special': 0.66; 'url:10': 0.72; '"all': 0.84; '2.7.1': 0.84; 'me).': 0.84; 'sender:addr:chris': 0.84; 'url:design': 0.84; 'url:rebertia': 0.84; 'jul': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rebertia.com; s=google; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=cUA9IbQnt8WHUrWcPFKmExKRkX10dHg4LR3YWRr6uk4=; b=UgllyiZQcFU0T5xmP6RhwtkvqXHxDTUMLxQ4TGKkKLwmJ22VScs5T1sIEQOWAD8Ofx Lo9mJwi5EdgpZ5X7ulMn9VJSCKXMmvU0JWRz5l+KBFon4xw0mB3X3YkCIM+Ws5qdV4de BKxuZve1s7ff59hF0vzPgEPbMNfPSRhLTctlU= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type :content-transfer-encoding:x-gm-message-state; bh=cUA9IbQnt8WHUrWcPFKmExKRkX10dHg4LR3YWRr6uk4=; b=WruUZrmJwAu6n+JtI4x2ccyL/k0itROIf8pksnzsbNWXXtZT9EBzE/cGh8QNUfT0VR s/AKjtvr7O7DTFZlGoBJboiT6I7XXGJnvKFaYLQJ2O1f9e55dgz/7xKJZOFb+RyRm7rr XzS+n4WgTEEcvu2eiHe8iDETcJaQtJ/zAm8zpAMUbtl7b/yaQXL/AvM1+O5uHzVKL2j1 OS8VfCJ0O4eBJOh6ZqyQDoEzlZOG8IoD9rwEIsl+xC/DOYLiNNKItqQBOk7vY2xqyCY6 lghnBra0pm08dTvpPRmdow9Mu0GVprQ3Fq2SChnfek+yidJYoGjcyuGUCZFfGAld3fZk /h0w== MIME-Version: 1.0 Sender: chris@rebertia.com In-Reply-To: References: Date: Thu, 22 Mar 2012 04:17:35 -0700 X-Google-Sender-Auth: uYrMGSbDQ29PgQPRUf29SY_nig0 Subject: Re: Python classes: Simplify? From: Chris Rebert To: Steven Lehar Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Gm-Message-State: ALoCoQnOhlnxkuwSvucx8wSkuGMcGnEp/cQl1XWtbeXvK3EaoGi+7F7a4ft/ALXS6ZrAnIh6xka7 Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 74 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1332415064 news.xs4all.nl 6902 [2001:888:2000:d::a6]:55344 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:22012 On Thu, Mar 22, 2012 at 3:51 AM, Steven Lehar wrote: > It seems to me that the Python class system is needlessly confusing. Am I > missing something? Explicit `self` is slightly annoying, but you'll get over it quickly (trust= me). > For example in the class Complex given in the documentation > > class Complex: > =C2=A0 =C2=A0 def __init__(self, realpart, imagpart): > =C2=A0 =C2=A0 =C2=A0 =C2=A0 self.r =3D realpart > =C2=A0 =C2=A0 =C2=A0 =C2=A0 self.i =3D imagpart > > x =3D Complex(3.0, -4.5) > > I initially found it profoundly confusing that __init__( ) calls for 3 > arguments, but you call Complex( ) with 2. That's not at all unique to __init__(). > Furthermore, why not call the > initialization function after the class name as is done in other language= s? Yech. That would add another step to renaming a class and make referring to the superclass initializer method rather difficult. It would also break the "all special methods have underscored names" rule. Perhaps you'll find the following instructive: Python 2.7.1 (r271:86832, Jul 31 2011, 19:30:53) >>> class Foo(object): ... def __init__(self): ... pass ... >>> Foo() <__main__.Foo object at 0x100fd5750> >>> Bar =3D Foo >>> Bar.__name__ =3D "Bar" >>> del Foo >>> # look Ma, I've renamed the class! >>> Bar() <__main__.Bar object at 0x100fd57d0> How would your scheme account for this possibility? > Isn't that the simplest conceptually? Demonstrating with the above exampl= e: > > class Complex: > =C2=A0 =C2=A0 def Complex(realpart, imagpart): > =C2=A0 =C2=A0 =C2=A0 =C2=A0 Complex.r =3D realpart > =C2=A0 =C2=A0 =C2=A0 =C2=A0 Complex.i =3D imagpart Your change of "self" to "Complex" in the method body here makes no sense t= o me. > x =3D Complex(3.0, -4.5) > > Is there a good reason why classes cannot be defined that way? (Besides t= he > problem of backward-compatibility) Mostly historical, IMO. But here are some justifications for explicit `self= `: http://docs.python.org/faq/design.html#why-must-self-be-used-explicitly-in-= method-definitions-and-calls http://neopythonic.blogspot.com/2008/10/why-explicit-self-has-to-stay.html Cheers, Chris R. -- http://blog.rebertia.com