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


Groups > comp.lang.python > #22011

Re: Python classes: Simplify?

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 <rosuav@gmail.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; 'attributes': 0.05; 'subject:Python': 0.05; 'c++,': 0.07; 'constructor': 0.07; 'python': 0.08; 'arguments,': 0.09; "object's": 0.09; 'rename': 0.09; 'def': 0.13; 'language,': 0.14; 'argument': 0.15; 'class,': 0.15; 'acted': 0.16; 'attributes.': 0.16; 'confusing.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'function?': 0.16; 'java.': 0.16; 'received:209.85.213.174': 0.16; 'received:mail- yx0-f174.google.com': 0.16; 'simplest': 0.16; 'subject:classes': 0.16; 'wrote:': 0.18; '>>>': 0.18; 'alternate': 0.18; 'seems': 0.20; 'java': 0.21; "doesn't": 0.22; 'header:In-Reply-To:1': 0.22; 'assigning': 0.23; 'implicit': 0.23; 'object,': 0.24; 'function': 0.27; 'message-id:@mail.gmail.com': 0.29; 'explicit': 0.29; 'class': 0.29; 'example': 0.29; 'pm,': 0.29; '22,': 0.30; '(the': 0.30; 'subject:?': 0.31; 'thu,': 0.32; 'does': 0.32; 'changing': 0.32; "isn't": 0.33; 'sort': 0.33; 'there': 0.33; "can't": 0.33; 'named': 0.33; 'object': 0.33; 'steven': 0.34; 'done': 0.34; 'especially': 0.34; 'latter': 0.34; 'to:addr:python-list': 0.35; 'things': 0.35; 'things,': 0.37; 'but': 0.37; 'received:google.com': 0.37; 'either': 0.37; 'skip:_ 10': 0.38; 'received:209.85': 0.38; 'too,': 0.38; 'initially': 0.39; 'option': 0.39; 'received:209': 0.39; 'called': 0.40; 'missing': 0.40; 'being': 0.40; 'change': 0.40; 'to:addr:python.org': 0.40; 'your': 0.61; 'world': 0.61; 'bear': 0.64; 'choices.': 0.84; 'distinguish': 0.84; 'done;': 0.84; 'foo:"': 0.84; 'does?': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type:content-transfer-encoding; bh=/tF2Kra632VUmf/VFL5ATNDGb5wXIF1JX5AwBBdL5cU=; b=hbYfswZNF6wFkeyp8wgGvkO6vga6xwRAu0FPAAeQMmmSE6efagbuMe6+//2nKVB6ve yWNRf2ZBDLWczMAsmbFg+qnWpO9Q16gISAC1myDtBmxHkOPb+aYBdj/LGODu7HsjauBP UuJKyc9zJKG1un9gYehX/ErBwGv6ycWAjfBWyDHpFhM25kQqSv5WTfLba6wmv9f1P5b+ W87gBNp2JAMv8zYRY5s5ztO9fGJaBoMi1PvY9JPgguNU8NSNz7YLnrGaCCUZVbjT0Jg9 zNF3oMhUY7V9I2d17kqPNBmVzj3flsSHL+Sqpx/Ne/D0DkTVqD0Lc9/J0aNvStPMIf9k TFOA==
MIME-Version 1.0
In-Reply-To <CAKz-UDNFpHF=2secHaFJ2M7LKAD4XtLLrqmE3uo1A6YVXH1zTg@mail.gmail.com>
References <CAKz-UDNFpHF=2secHaFJ2M7LKAD4XtLLrqmE3uo1A6YVXH1zTg@mail.gmail.com>
Date Thu, 22 Mar 2012 22:10:53 +1100
Subject Re: Python classes: Simplify?
From Chris Angelico <rosuav@gmail.com>
To python-list@python.org
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding quoted-printable
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.880.1332414655.3037.python-list@python.org> (permalink)
Lines 63
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1332414655 news.xs4all.nl 6949 [2001:888:2000:d::a6]:52612
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:22011

Show key headers only | View raw


On Thu, Mar 22, 2012 at 9:51 PM, Steven Lehar <slehar@gmail.com> 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:

Why doesn't Python do things the way Java does? Because Python isn't
Java. Different language, different choices. :)

Methods are called with an explicit first argument (the object being
acted upon). Constructors need that argument too, even though it's not
given in the invocation. With your example alternate syntax:

> class Complex:
>     def Complex(realpart, imagpart):
>         Complex.r = realpart
>         Complex.i = imagpart

there's no way to distinguish between assigning to the newly-created
object's attributes and assigning to the class's own attributes.
Especially since the latter is a truly viable option in Python, this
can't be done; consequently, there needs to be either an explicit or
an implicit "self" argument (C++ does this with an implicit 'this'
pointer); the Python choice is to make it explicit.

There's really no reason to have the name of the constructor change
when the class is renamed. The way Python does things, you can rename
a class by changing just one thing, the "class Foo:" line. In C++, you
have to also rename all your constructors. And bear in mind, a class
is an object, same as any other:

>>> class Foo:
	def __init__(self,x):
		self.x=x
	def speak(self):
		print("x = "+self.x)
		
>>> a=Foo("Hello")
>>> a.speak()
x = Hello
>>> Bar=Foo
>>> b=Bar("World")
>>> b.speak()
x = World

If the constructor is named the same as the class, how would this sort
of thing function?

ChrisA

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


Thread

Re: Python classes: Simplify? Chris Angelico <rosuav@gmail.com> - 2012-03-22 22:10 +1100

csiph-web