Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #22073

Re: Python classes: Simplify?

Path csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <prvs=422f902cc=jeanmichel@sequans.com>
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; 'python,': 0.01; 'example:': 0.03; 'distinct': 0.04; 'subject:Python': 0.05; 'python': 0.08; 'arguments,': 0.09; 'instance.': 0.09; 'invokes': 0.09; 'def': 0.13; 'model,': 0.15; '(besides': 0.16; '*class': 0.16; '__init__': 0.16; 'confusing.': 0.16; 'simplest': 0.16; 'subject:classes': 0.16; 'cc:addr:python-list': 0.16; 'syntax': 0.16; 'wrote:': 0.18; 'exists': 0.18; 'instance': 0.18; 'seems': 0.20; 'cc:no real name:2**0': 0.21; 'header:In-Reply-To:1': 0.22; '"it\'s': 0.23; 'translated': 0.23; 'way?': 0.23; 'defined': 0.24; 'cc:2**0': 0.26; 'classes': 0.26; 'function': 0.27; 'not.': 0.28; 'explicit': 0.29; 'class': 0.29; 'example': 0.29; 'problem': 0.29; 'skip:b 20': 0.29; 'cc:addr:python.org': 0.29; 'subject:?': 0.31; 'familiar': 0.32; 'idea': 0.32; "isn't": 0.33; 'there': 0.33; "won't": 0.33; 'header:User-Agent:1': 0.33; 'steven': 0.34; 'done': 0.34; 'jump': 0.34; 'something': 0.35; 'with.': 0.37; 'but': 0.37; 'skip:" 10': 0.37; 'using': 0.37; 'skip:_ 10': 0.38; 'uses': 0.38; 'data': 0.38; 'initially': 0.39; 'cannot': 0.39; 'called': 0.40; 'missing': 0.40; "you'll": 0.61; 'conclusions': 0.84
X-IronPort-AV E=Sophos;i="4.73,637,1325458800"; d="scan'208";a="269724"
X-Virus-Scanned amavisd-new at zimbra.sequans.com
Date Fri, 23 Mar 2012 15:19:55 +0100
From Jean-Michel Pichavant <jeanmichel@sequans.com>
User-Agent Mozilla-Thunderbird 2.0.0.24 (X11/20100328)
MIME-Version 1.0
To Steven Lehar <slehar@gmail.com>
Subject Re: Python classes: Simplify?
References <CAKz-UDNFpHF=2secHaFJ2M7LKAD4XtLLrqmE3uo1A6YVXH1zTg@mail.gmail.com>
In-Reply-To <CAKz-UDNFpHF=2secHaFJ2M7LKAD4XtLLrqmE3uo1A6YVXH1zTg@mail.gmail.com>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
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 <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.924.1332512488.3037.python-list@python.org> (permalink)
Lines 51
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1332512488 news.xs4all.nl 6934 [2001:888:2000:d::a6]:47727
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:22073

Show key headers only | View raw


Steven Lehar wrote:
> It seems to me that the Python class system is needlessly confusing. 
> Am I missing something?
>
> For example in the class Complex given in the documentation
>
> *class Complex:*
> *    def __init__(self, realpart, imagpart):*
> *        self.r = realpart*
> *        self.i = imagpart*
> *
> *
> *x = Complex(3.0, -4.5)*
>
> I initially found it profoundly confusing that __init__( ) calls for 3 
> arguments, but you call Complex( ) with 2. Furthermore, why not call 
> the initialization function after the class name as is done in other 
> languages? Isn't that the simplest conceptually? Demonstrating with 
> the above example:
>

In python, writting obj.method() will be translated into method(obj) so 
any instance method has a #arg + 1 arguments, something you'll get 
familiar with.
Furthermore, Complex(3.0, -4.5) invokes 2 functions : __new__ and 
__init__. __new__ is the "constructor", this is the function that 
returns an instance. __init__ is an initializer, at the time it's called 
the instance already exists and is viable.


> *class Complex:*
> *    def Complex(realpart, imagpart):*
> *        Complex.r = realpart*
> *        Complex.i = imagpart*
> *
> *
> *x = Complex(3.0, -4.5)*
> *
> *
> Is there a good reason why classes cannot be defined that way? 
> (Besides the problem of backward-compatibility)
>


Python uses a different data model, it is a very good idea to mark 
theses differences using an explicit distinct syntax so that people 
won't jump into false conclusions like "it's like C or Java". It is not.

JM

JM

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Python classes: Simplify? Jean-Michel Pichavant <jeanmichel@sequans.com> - 2012-03-23 15:19 +0100

csiph-web