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


Groups > comp.lang.python > #22073 > unrolled thread

Re: Python classes: Simplify?

Started byJean-Michel Pichavant <jeanmichel@sequans.com>
First post2012-03-23 15:19 +0100
Last post2012-03-23 15:19 +0100
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

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

#22073 — Re: Python classes: Simplify?

FromJean-Michel Pichavant <jeanmichel@sequans.com>
Date2012-03-23 15:19 +0100
SubjectRe: Python classes: Simplify?
Message-ID<mailman.924.1332512488.3037.python-list@python.org>
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

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web