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


Groups > comp.lang.python > #16655

Re: Fwd: class print method...

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!newsfeed.eweka.nl!eweka.nl!feeder3.eweka.nl!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.003
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'oct': 0.02; 'string.': 0.04; 'prints': 0.07; 'typed': 0.07; 'python': 0.08; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'thanx': 0.09; 'output': 0.10; 'def': 0.13; 'stored': 0.13; '"copyright",': 0.16; '"credits"': 0.16; '"license"': 0.16; '[gcc': 0.16; 'class;': 0.16; 'deck': 0.16; 'linux2': 0.16; 'repr': 0.16; 'subject:print': 0.16; 'wrote:': 0.18; '>>>': 0.18; 'string,': 0.18; 'memory': 0.21; 'header:In-Reply-To:1': 0.22; '----------': 0.23; 'itself,': 0.23; 'pair': 0.23; 'string': 0.24; 'shell': 0.24; 'suspect': 0.24; 'modify': 0.25; 'code': 0.25; 'script.': 0.29; 'date:': 0.29; 'print': 0.29; 'pm,': 0.29; 'class': 0.29; 'track,': 0.30; 'objects': 0.32; 'sort': 0.33; 'header:User- Agent:1': 0.33; 'header:X-Complaints-To:1': 0.33; 'instead': 0.33; 'there': 0.33; 'object': 0.33; 'to:addr:python-list': 0.34; 'subject:': 0.34; 'running': 0.35; 'explain': 0.36; 'received:au': 0.36; 'uses': 0.36; 'class.': 0.37; 'response,': 0.37; 'but': 0.37; 'run': 0.37; "there's": 0.37; 'think': 0.37; 'skip:_ 10': 0.37; 'monday,': 0.38; 'suit': 0.38; 'received:org': 0.38; 'created': 0.38; 'getting': 0.38; 'correctly': 0.39; 'from:': 0.39; 'either': 0.39; 'why': 0.39; 'help': 0.39; 'to:addr:python.org': 0.40; 'more': 0.61; 'type': 0.61; 'quick': 0.61; '2011': 0.61; 'your': 0.61; 'below': 0.63; 'card': 0.65; 'android': 0.66; 'hurry': 0.67; 'enclosed': 0.68; "'2',": 0.84; "'3',": 0.84; 'card:': 0.84; 'rank,': 0.84; 'ranks': 0.84; 'hearts': 0.91; 'suresh': 0.91; 'rank': 0.93; 'received:110': 0.95
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Lie Ryan <lie.1296@gmail.com>
Subject Re: Fwd: class print method...
Date Mon, 05 Dec 2011 23:41:25 +1100
References <CALKjt2fen-uZ==A=KJ=-C8JAufU4vmeZQqyW1=3sWR6ajOpXhw@mail.gmail.com> <4EDC775D.4000906@davea.name> <CALKjt2dTF=k-2pQy7YQPsEWexWGK5ej5w721Es+BtQnnoHghDg@mail.gmail.com> <CALKjt2caxDh_GBwtBaNDzF6=Q=tPpyNe4db1BeUX16zda-XvhQ@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host 110-175-240-90.static.tpgi.com.au
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:8.0) Gecko/20111124 Thunderbird/8.0
In-Reply-To <CALKjt2caxDh_GBwtBaNDzF6=Q=tPpyNe4db1BeUX16zda-XvhQ@mail.gmail.com>
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.3299.1323088902.27778.python-list@python.org> (permalink)
Lines 57
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1323088902 news.xs4all.nl 6970 [2001:888:2000:d::a6]:51496
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:16655

Show key headers only | View raw


On 12/05/2011 10:18 PM, Suresh Sharma wrote:
>
> Pls help its really frustrating
> ---------- Forwarded message ----------
> From: Suresh Sharma
> Date: Monday, December 5, 2011
> Subject: class print method...
> To: "d@davea.name <mailto:d@davea.name>" <d@davea.name
> <mailto:d@davea.name>>
>
>
> Dave,
> Thanx for the quick response, i am sorry that i did not explain
> correctly look at the code below inspite of this i am just getting class
> object at memory location.I am sort i typed all this code on my android
> in a hurry so.indentation could.not.be.managed but this.similar code
> when i run all my objects created by class deck are not shown but stored
> in varioia meory locations. How can i display them.
>

I think you're in the right track, however I suspect you're running the 
code in the shell instead of as a script. The shell uses __repr__() to 
print objects instead of __str__(), so you either need to use 'print' or 
you need to call str(), note the following:

Python 2.7.2+ (default, Oct  4 2011, 20:06:09)
[GCC 4.6.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
 >>> suits = ['spades', 'clubs', 'diamonds', 'hearts']
 >>> ranks = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 
'Q', 'K']
 >>> class Card:
...     def __init__(self, rank, suit):
...         self.suit = suit
...         self.rank = rank
...     def __str__(self):
...         return suits[self.suit] + ' ' + ranks[self.rank]
...
 >>> Card(2, 3) #1
<__main__.Card instance at 0x7f719c3a20e0>
 >>> str(Card(2, 3)) #2 of your
'hearts 3'
 >>> print Card(2, 3) #3
hearts 3

In #1, the output is the __repr__() of your Card class; you can modify 
this output by overriding the __repr__() on your Card class.

In #2, the output is the __repr__() of a string, the string is the 
return value from __str__() of your Card class. The repr of a string is 
the string enclosed in quotes, which is why there is an extra pair of 
quotes.

In #3, you're 'print'-ing a string, the string is the return value from 
__str__() of your Card class. There's no extra quotes, since 'print' 
prints the string itself, not the repr of the string.

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


Thread

Re: Fwd: class print method... Lie Ryan <lie.1296@gmail.com> - 2011-12-05 23:41 +1100

csiph-web