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


Groups > comp.lang.python > #39758

Re: intX.__str__() ??

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <torriem@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.008
X-Spam-Evidence '*H*': 0.98; '*S*': 0.00; 'operator': 0.03; 'method.': 0.05; 'skip:` 10': 0.05; 'ugly': 0.07; 'python': 0.09; 'notation': 0.09; 'underscore': 0.09; 'language': 0.14; 'break!': 0.16; 'from:addr:torriem': 0.16; 'from:name:michael torrie': 0.16; 'guys,': 0.16; 'performed.': 0.16; 'underscores': 0.16; 'string': 0.17; 'wrote:': 0.17; "shouldn't": 0.17; 'code.': 0.20; 'define': 0.20; 'question.': 0.20; 'written': 0.20; 'init': 0.22; 'example': 0.23; 'for?': 0.23; 'header:In-Reply-To:1': 0.25; 'header:User- Agent:1': 0.26; 'am,': 0.27; 'wonder': 0.27; 'convert': 0.29; 'skip:_ 10': 0.29; 'probably': 0.29; 'class': 0.29; "i'm": 0.29; 'code': 0.31; 'not.': 0.32; 'skip:_ 30': 0.32; "he's": 0.33; 'instances': 0.33; 'int': 0.33; 'null': 0.33; 'to:addr:python- list': 0.33; 'another': 0.33; 'received:org': 0.36; 'explain': 0.36; 'message-id:@gmail.com': 0.36; 'should': 0.36; 'uses': 0.37; 'being': 0.37; 'why': 0.37; 'maintaining': 0.37; 'subject:: ': 0.38; 'things': 0.38; 'sure': 0.38; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'received:192.168': 0.40; 'header:Received:5': 0.40; 'think': 0.40; 'matter': 0.61; 'first': 0.61; 'course.': 0.62; 'email addr:gmail.com': 0.63; 'introduce': 0.80; '(is': 0.84; 'furman': 0.84; 'overloading': 0.84; 'starters,': 0.84; 'ethan': 0.91; 'habit': 0.91; 'hungarian': 0.91
X-Virus-Scanned amavisd-new at torriefamily.org
Date Sun, 24 Feb 2013 10:29:24 -0700
From Michael Torrie <torriem@gmail.com>
User-Agent Mozilla/5.0 (X11; Linux i686; rv:10.0.12) Gecko/20130105 Thunderbird/10.0.12
MIME-Version 1.0
To python-list@python.org
Subject Re: intX.__str__() ??
References <512A3E85.6000307@stoneleaf.us>
In-Reply-To <512A3E85.6000307@stoneleaf.us>
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding 7bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
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.2416.1361726973.2939.python-list@python.org> (permalink)
Lines 36
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1361726973 news.xs4all.nl 6911 [2001:888:2000:d::a6]:40260
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:39758

Show key headers only | View raw


On 02/24/2013 09:23 AM, Ethan Furman wrote:
> On 02/24/2013 07:46 AM, piterrr.dolinski@gmail.com wrote:> Hi guys,
>>
>> Question. Have this code
>>
>> intX = 32                          # decl + init int var
>> intX_asString = None               # decl + init with NULL string var
>>
>> intX_asString = intX.__str__ ()    # convert int to string
>>
>> What are these ugly underscores for? _________________str___________________
> 
> This is a good example of why you shouldn't program language X in language Y.
> 
> For starters, `intX.__str__` should be written as `str(intX)`;
> 
> For middlers, intX_asString is probably not necessary (is it being printed? then
> do a `print intX`, or a `print "size left on disk: %d" % intX`, etc.
> 
> For finishers, why the System Hungarian Notation?

I think he's maintaining existing code.  It's unfortunate that his first
exposure to python is code written by someone else in such a poor style,
and in a way that definitely isn't pythonic.  No wonder he's struggling
to like python!  Though I'm sure since his recent experience has been
exclusively in C# that he probably uses hungarian notation as a matter
of course.  A hard habit to break!  Is this a good time to introduce him
to duck typing?  Probably not.

Another way to explain the double underscore methods is that they are
how things like operator overloading is performed.  Want to make a class
that you can use the [index] notation on instances? Define the
__get_attr__() method.  And to define a class that you can then use
instances with the + operator?  Define the __add__() method.  This is a
good introduction:
http://getpython3.com/diveintopython3/special-method-names.html

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


Thread

Re: intX.__str__() ?? Michael Torrie <torriem@gmail.com> - 2013-02-24 10:29 -0700

csiph-web