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


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

Can you use self in __str__

Started bySeymore4Head <Seymore4Head@Hotmail.invalid>
First post2014-11-27 20:26 -0500
Last post2014-11-29 11:55 +0800
Articles 16 — 6 participants

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


Contents

  Can you use self in __str__ Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-11-27 20:26 -0500
    Re: Can you use self in __str__ Dave Angel <davea@davea.name> - 2014-11-27 21:49 -0500
      Re: Can you use self in __str__ Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-11-27 22:31 -0500
        Re: Can you use self in __str__ Dave Angel <davea@davea.name> - 2014-11-28 04:37 -0500
    Re: Can you use self in __str__ Shiyao Ma <i@introo.me> - 2014-11-28 11:04 +0800
      Re: Can you use self in __str__ Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-11-27 22:35 -0500
        Re: Can you use self in __str__ Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-11-28 18:44 +1100
    Re: Can you use self in __str__ Chris Angelico <rosuav@gmail.com> - 2014-11-28 16:00 +1100
    Re: Can you use self in __str__ Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-11-28 19:25 +1100
    Re: Can you use self in __str__ Chris Angelico <rosuav@gmail.com> - 2014-11-28 12:43 +1100
    Re: Can you use self in __str__ Dave Angel <davea@davea.name> - 2014-11-28 06:43 -0500
    Re: Can you use self in __str__ Rustom Mody <rustompmody@gmail.com> - 2014-11-28 06:21 -0800
      Re: Can you use self in __str__ Rustom Mody <rustompmody@gmail.com> - 2014-11-28 08:37 -0800
    Re: Can you use self in __str__ Shiyao Ma <i@introo.me> - 2014-11-29 11:16 +0800
    Re: Can you use self in __str__ Chris Angelico <rosuav@gmail.com> - 2014-11-29 14:36 +1100
    Re: Can you use self in __str__ Shiyao Ma <i@introo.me> - 2014-11-29 11:55 +0800

#81657 — Can you use self in __str__

FromSeymore4Head <Seymore4Head@Hotmail.invalid>
Date2014-11-27 20:26 -0500
SubjectCan you use self in __str__
Message-ID<9oif7apuou9fbaqjk0i3l2pq1theuso4un@4ax.com>
    def __str__(self):
        s = "Hand contains "
        for x in self.hand:
            s = s + str(x) + " "
        return s

This is part of a Hand class.  I need a hand for the dealer and a hand
for the player.
dealer=Hand()
player=Hand()
This prints out 'Hand contains " foo bar 
for both the dealer's hand and the player's hand.

Is there a way to include "self" in the __string__ so it reads
Dealer hand contains foo bar
Player hand contains foo bar

I tried:
s  = self + "Hand contains "  and that doesn't work.
s = self + s + str(x) + " "  doesn't work either
Line 60: TypeError: cannot concatenate 'str' and 'Hand' objects

I tried:
s = self.hand + s + str(x) + " "
Line 60: TypeError: can only concatenate list to list

[toc] | [next] | [standalone]


#81658

FromDave Angel <davea@davea.name>
Date2014-11-27 21:49 -0500
Message-ID<mailman.16388.1417142987.18130.python-list@python.org>
In reply to#81657
On 11/27/2014 08:26 PM, Seymore4Head wrote:
>      def __str__(self):
>          s = "Hand contains "
>          for x in self.hand:
>              s = s + str(x) + " "
>          return s
>
> This is part of a Hand class.  I need a hand for the dealer and a hand
> for the player.
> dealer=Hand()
> player=Hand()

Good so far. But you neglect to tell us Python version.

> This

What's "this" ?  Please include real code, not just vague suggestions.

> prints out 'Hand contains " foo bar
> for both the dealer's hand and the player's hand.
>
> Is there a way to include "self" in the __string__ so it reads
> Dealer hand contains foo bar
> Player hand contains foo bar

You don't show us the __init__ method.  That's where you should 
establish a "name" for the particular instance of Hand.

class Hand(object):
     def __init__(self, name):
         self.name = name
         self.hand =   something ...

     def __str__(self):
           s = self.name + " contains "
           for x in self.hand:
               s = s + str(x) + " "
           return s


When you quote an error, you should be including the whole thing, as 
well as showing the code that triggers it.  In this message you're 
giving us only disjoint fragments, and one line summary of the traceback.

-- 
DaveA

[toc] | [prev] | [next] | [standalone]


#81660

FromSeymore4Head <Seymore4Head@Hotmail.invalid>
Date2014-11-27 22:31 -0500
Message-ID<4gpf7ahkq3isr8g7l6cs8dt0anu9oiva57@4ax.com>
In reply to#81658
On Thu, 27 Nov 2014 21:49:29 -0500, Dave Angel <davea@davea.name>
wrote:

class Hand:
    def __init__(self):
        self.hand = []
        # create Hand object

    def __str__(self):
        s = 'Hand contains '
        for x in self.hand:
            s = s + str(x) + " "
        return s

I am using 2.7 (Codeskulptor).  This is working code.  It starts with
an empty list that gets appended from a full deck of shuffled cards.
dealer=Hand()
player=Hand()
I don't really know how to post working code without posting a lot.  I
am not being too successful in trying to post enough code to have it
work without posting the entire code.
Here is the link if you want to run it.
http://www.codeskulptor.org/#user38_Kka7mh2v9u_9.py
The print out looks like this:
Hand contains H4 DQ.

I can (and am) currently printing the hand like this:
print "Player's",player
print "Dealer's",dealer

My question is can you add (self) in the __str__ so when you issue the
command "print player" the "player" part is included in the __str__.

[toc] | [prev] | [next] | [standalone]


#81673

FromDave Angel <davea@davea.name>
Date2014-11-28 04:37 -0500
Message-ID<mailman.16398.1417167475.18130.python-list@python.org>
In reply to#81660
On 11/27/2014 10:31 PM, Seymore4Head wrote:
> On Thu, 27 Nov 2014 21:49:29 -0500, Dave Angel <davea@davea.name>
> wrote:
>
> class Hand:
>      def __init__(self):
>          self.hand = []
>          # create Hand object
>
>      def __str__(self):
>          s = 'Hand contains '
>          for x in self.hand:
>              s = s + str(x) + " "
>          return s
>
> I am using 2.7 (Codeskulptor).  This is working code.  It starts with
> an empty list that gets appended from a full deck of shuffled cards.
> dealer=Hand()
> player=Hand()
> I don't really know how to post working code without posting a lot.  I
> am not being too successful in trying to post enough code to have it
> work without posting the entire code.
> Here is the link if you want to run it.
> http://www.codeskulptor.org/#user38_Kka7mh2v9u_9.py
> The print out looks like this:
> Hand contains H4 DQ.
>
> I can (and am) currently printing the hand like this:
> print "Player's",player
> print "Dealer's",dealer
>
> My question is can you add (self) in the __str__ so when you issue the
> command "print player" the "player" part is included in the __str__.
>

You've already got self in the __str__ method, or you wouldn't have 
access to self.hand.  But there's no characteristic of 'self' that has 
any idea of a name like "dealer" or "player".  You have to add that if 
you want it, as I suggested in my first guess.  Steven has shown you as 
well, along with a better explanation.

An object does NOT know the name or names that may be bound to it, any 
more than I know what page of the county register has my birth 
certificate recorded.  If I want to know my own name, I'd better 
remember it.  Along with any nicknames I want to respond to.  The way to 
do it is the same way to know the hand that I hold, make an instance 
attribute.  And the place to do that is in the __init__() method.


class Hand:
     def __init__(self, myname):
         self.hand = []
         # create Hand object
	self.name = myname

     def __str__(self):
         s = self.name + ' contains '
         for x in self.hand:
             s = s + str(x) + " "
         return s


dealer=Hand("Dealer")
player=Hand("Player")



-- 
DaveA

[toc] | [prev] | [next] | [standalone]


#81659

FromShiyao Ma <i@introo.me>
Date2014-11-28 11:04 +0800
Message-ID<mailman.16389.1417143874.18130.python-list@python.org>
In reply to#81657
2014-11-28 9:26 GMT+08:00 Seymore4Head <Seymore4Head@hotmail.invalid>:
>     def __str__(self):
>         s = "Hand contains "
>         for x in self.hand:
>             s = s + str(x) + " "
>         return s
>
> This is part of a Hand class.  I need a hand for the dealer and a hand
> for the player.
> dealer=Hand()
> player=Hand()
> This prints out 'Hand contains " foo bar
> for both the dealer's hand and the player's hand.
>
> Is there a way to include "self" in the __string__ so it reads
> Dealer hand contains foo bar
> Player hand contains foo bar

I bet you want the object name (aka, dealer, player) be included in
the string 's'.
To that end, you need to access the namespace where 'self' is in.
But I dunno where the namespace 'self' resides in.
Does PyObject has a field to store the namespace of an object?
Appreciated if anyone could
inform me on this.

Now, make a little assumption that the instance lives in the module
level. Then we can do
this:

#!/usr/bin/env python

class Hand(object):
def __init__(self):
self.hand = [1, 2, 3, 4]

def __str__(self):
s = self._myname + " hand contains "
for x in self.hand:
s = s + str(x) + " "
return s

@property
def _myname(self):
# get the module
mod = self.__module__
import sys
ns = vars(sys.modules[mod])
# NB only works the instance is at the module level
for name, obj in ns.iteritems():
if id(obj) == id(self):
break
else:
#nothing found
return ""
return name

John = Hand()
print(John)

# this prints
# John hand contains 1 2 3 4

bad indentation with my wasavi plugin, see paste:

https://bpaste.net/show/f5b86957295f


What if it's in the local namespace of a function or method? IDK, try
to get that thing first.


Regards






--

吾輩は猫である。ホームーページはhttp://introo.me。

[toc] | [prev] | [next] | [standalone]


#81661

FromSeymore4Head <Seymore4Head@Hotmail.invalid>
Date2014-11-27 22:35 -0500
Message-ID<b6rf7admi10dnub1pq1bebv3f967kn0okm@4ax.com>
In reply to#81659
On Fri, 28 Nov 2014 11:04:26 +0800, Shiyao Ma <i@introo.me> wrote:

>2014-11-28 9:26 GMT+08:00 Seymore4Head <Seymore4Head@hotmail.invalid>:
>>     def __str__(self):
>>         s = "Hand contains "
>>         for x in self.hand:
>>             s = s + str(x) + " "
>>         return s
>>
>> This is part of a Hand class.  I need a hand for the dealer and a hand
>> for the player.
>> dealer=Hand()
>> player=Hand()
>> This prints out 'Hand contains " foo bar
>> for both the dealer's hand and the player's hand.
>>
>> Is there a way to include "self" in the __string__ so it reads
>> Dealer hand contains foo bar
>> Player hand contains foo bar
>
>I bet you want the object name (aka, dealer, player) be included in
>the string 's'.

That is exactly what I want, but your explanation is too complicated
for my feeble mind to get just yet.

>To that end, you need to access the namespace where 'self' is in.
>But I dunno where the namespace 'self' resides in.
>Does PyObject has a field to store the namespace of an object?
>Appreciated if anyone could
>inform me on this.
>
>Now, make a little assumption that the instance lives in the module
>level. Then we can do
>this:
>
>#!/usr/bin/env python
>
>class Hand(object):
>def __init__(self):
>self.hand = [1, 2, 3, 4]
>
>def __str__(self):
>s = self._myname + " hand contains "
>for x in self.hand:
>s = s + str(x) + " "
>return s
>
>@property
>def _myname(self):
># get the module
>mod = self.__module__
>import sys
>ns = vars(sys.modules[mod])
># NB only works the instance is at the module level
>for name, obj in ns.iteritems():
>if id(obj) == id(self):
>break
>else:
>#nothing found
>return ""
>return name
>
>John = Hand()
>print(John)
>
># this prints
># John hand contains 1 2 3 4
>
>bad indentation with my wasavi plugin, see paste:
>
>https://bpaste.net/show/f5b86957295f
>
>
>What if it's in the local namespace of a function or method? IDK, try
>to get that thing first.
>
>
>Regards

[toc] | [prev] | [next] | [standalone]


#81671

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2014-11-28 18:44 +1100
Message-ID<547827d9$0$12978$c3e8da3$5496439d@news.astraweb.com>
In reply to#81661
Seymore4Head wrote:

> On Fri, 28 Nov 2014 11:04:26 +0800, Shiyao Ma <i@introo.me> wrote:
> 
>>2014-11-28 9:26 GMT+08:00 Seymore4Head <Seymore4Head@hotmail.invalid>:
>>>     def __str__(self):
>>>         s = "Hand contains "
>>>         for x in self.hand:
>>>             s = s + str(x) + " "
>>>         return s
>>>
>>> This is part of a Hand class.  I need a hand for the dealer and a hand
>>> for the player.
>>> dealer=Hand()
>>> player=Hand()
>>> This prints out 'Hand contains " foo bar
>>> for both the dealer's hand and the player's hand.
>>>
>>> Is there a way to include "self" in the __string__ so it reads
>>> Dealer hand contains foo bar
>>> Player hand contains foo bar
>>
>>I bet you want the object name (aka, dealer, player) be included in
>>the string 's'.
> 
> That is exactly what I want, but your explanation is too complicated
> for my feeble mind to get just yet.


Shiyao Ma's explanation is too complicated for *my* feeble mind to get, and
I've been using Python for over 15 years. Either I have no idea what Ma is
trying to say, or Ma has no idea what (s)he's trying to say.

:-)


>>To that end, you need to access the namespace where 'self' is in.

`self` is a local variable of the method. Accessing the local namespace is
automatic.


-- 
Steven

[toc] | [prev] | [next] | [standalone]


#81664

FromChris Angelico <rosuav@gmail.com>
Date2014-11-28 16:00 +1100
Message-ID<mailman.16391.1417150852.18130.python-list@python.org>
In reply to#81657
On Fri, Nov 28, 2014 at 2:04 PM, Shiyao Ma <i@introo.me> wrote:
> What if it's in the local namespace of a function or method? IDK, try
> to get that thing first.

What if it's in multiple namespaces? What if it's not in any at all?
Your solution is not going to work in the general case, AND it's a bad
idea. Please don't do this in any code that I'll ever have to
maintain.

ChrisA

[toc] | [prev] | [next] | [standalone]


#81672

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2014-11-28 19:25 +1100
Message-ID<54783193$0$12995$c3e8da3$5496439d@news.astraweb.com>
In reply to#81657
Seymore4Head wrote:

>     def __str__(self):
>         s = "Hand contains "
>         for x in self.hand:
>             s = s + str(x) + " "
>         return s
> 
> This is part of a Hand class.  I need a hand for the dealer and a hand
> for the player.
> dealer=Hand()
> player=Hand()
> This prints out 'Hand contains " foo bar
> for both the dealer's hand and the player's hand.
> 
> Is there a way to include "self" in the __string__ so it reads
> Dealer hand contains foo bar
> Player hand contains foo bar

Not unless you tell the instance what name you want it to use.

Instances (objects) have no way of knowing the name of the variable you
attach them to. Or even if there is such a variable -- there could be one,
or none, or a thousand.

Consider:

# one variable, one instance
dealer = Hand()

# three variables, one instance
player1 = player2 = player3 = Hand()

# make that four variables
player4 = player2

# no variable, one instance
print Hand()

some_list = [1, 2, 3, Hand(), 5]


If you ask each instance what their name is, how would they know?


The only way is to give them a name when you create them:

class Hand:
    def __init__(self, name):
        self.name = name


dealer = Hand("dealer")
player1 = Hand("Bert")
player2 = Hand("Ernie")
player3 = Hand("Scarface")


Now the instances know what their name is, since you've told them, and can
use them any way you like:

    def __str__(self):
        template = "%s's hand contains: "
        return (template % self.name) + "King of Spades"



-- 
Steven

[toc] | [prev] | [next] | [standalone]


#81675

FromChris Angelico <rosuav@gmail.com>
Date2014-11-28 12:43 +1100
Message-ID<mailman.16400.1417172816.18130.python-list@python.org>
In reply to#81657
On Fri, Nov 28, 2014 at 12:26 PM, Seymore4Head
<Seymore4Head@hotmail.invalid> wrote:
> dealer=Hand()
> player=Hand()
> This prints out 'Hand contains " foo bar
> for both the dealer's hand and the player's hand.
>
> Is there a way to include "self" in the __string__ so it reads
> Dealer hand contains foo bar
> Player hand contains foo bar

No, you can't. You're assuming that the name bound to an object is
somehow part of that object, but it isn't. What would happen if you
did this:

print(Hand())

There's no name, so you can't get that information. The only way to do
it would be to pass that to the Hand object, but you may as well
simply print it out separately.

ChrisA

[toc] | [prev] | [next] | [standalone]


#81676

FromDave Angel <davea@davea.name>
Date2014-11-28 06:43 -0500
Message-ID<mailman.16401.1417176578.18130.python-list@python.org>
In reply to#81657
On 11/27/2014 08:43 PM, Chris Angelico wrote:
> On Fri, Nov 28, 2014 at 12:26 PM, Seymore4Head
> <Seymore4Head@hotmail.invalid> wrote:
>> dealer=Hand()
>> player=Hand()
>> This prints out 'Hand contains " foo bar
>> for both the dealer's hand and the player's hand.
>>
>> Is there a way to include "self" in the __string__ so it reads
>> Dealer hand contains foo bar
>> Player hand contains foo bar
>
> No, you can't. You're assuming that the name bound to an object is
> somehow part of that object, but it isn't. What would happen if you
> did this:
>
> print(Hand())
>
> There's no name, so you can't get that information. The only way to do
> it would be to pass that to the Hand object, but you may as well
> simply print it out separately.

It is very useful for a Hand instance to know its "name".

Lots of game strategies treat a collection of such objects identically, 
and then after the fact want to tell them apart.  Printing is one 
example.  But if you've just concluded that object 174fa44 is the 
winner, it'd be nice to be able to tell the user in his own terms.

As you say you shouldn't attempt to guess it, but should pass it into 
the initializer.
-- 
DaveA

[toc] | [prev] | [next] | [standalone]


#81682

FromRustom Mody <rustompmody@gmail.com>
Date2014-11-28 06:21 -0800
Message-ID<3b164400-a7e2-4b7c-8dfa-b94db8fcfc65@googlegroups.com>
In reply to#81657
On Friday, November 28, 2014 6:57:23 AM UTC+5:30, Seymore4Head wrote:
> def __str__(self):
>         s = "Hand contains "
>         for x in self.hand:
>             s = s + str(x) + " "
>         return s
> 
> This is part of a Hand class.  I need a hand for the dealer and a hand
> for the player.
> dealer=Hand()
> player=Hand()
> This prints out 'Hand contains " foo bar 
> for both the dealer's hand and the player's hand.
> 
> Is there a way to include "self" in the __string__ so it reads
> Dealer hand contains foo bar
> Player hand contains foo bar

And what do you think should be printed if instead of your code

dealer = Hand()
player = Hand()

we have the following

dealer = Hand()
player = dealer

??

[toc] | [prev] | [next] | [standalone]


#81692

FromRustom Mody <rustompmody@gmail.com>
Date2014-11-28 08:37 -0800
Message-ID<e0065944-2f95-4687-889c-279bf804c1ae@googlegroups.com>
In reply to#81682
On Friday, November 28, 2014 7:51:40 PM UTC+5:30, Rustom Mody wrote:
> On Friday, November 28, 2014 6:57:23 AM UTC+5:30, Seymore4Head wrote:
> > def __str__(self):
> >         s = "Hand contains "
> >         for x in self.hand:
> >             s = s + str(x) + " "
> >         return s
> > 
> > This is part of a Hand class.  I need a hand for the dealer and a hand
> > for the player.
> > dealer=Hand()
> > player=Hand()
> > This prints out 'Hand contains " foo bar 
> > for both the dealer's hand and the player's hand.
> > 
> > Is there a way to include "self" in the __string__ so it reads
> > Dealer hand contains foo bar
> > Player hand contains foo bar
> 
> And what do you think should be printed if instead of your code
> 
> dealer = Hand()
> player = Hand()
> 
> we have the following
> 
> dealer = Hand()
> player = dealer
> 
> ??

Sorry for repeating -- I see Steven said the same

[toc] | [prev] | [next] | [standalone]


#81709

FromShiyao Ma <i@introo.me>
Date2014-11-29 11:16 +0800
Message-ID<mailman.16422.1417231400.18130.python-list@python.org>
In reply to#81657
2014-11-28 13:00 GMT+08:00 Chris Angelico <rosuav@gmail.com>:
> On Fri, Nov 28, 2014 at 2:04 PM, Shiyao Ma <i@introo.me> wrote:
>> What if it's in the local namespace of a function or method? IDK, try
>> to get that thing first.
>
Sure enough. I will even avoid using "id" as it's dependent on CPython
implementation. :)

> What if it's in multiple namespaces? What if it's not in any at all?
> Your solution is not going to work in the general case, AND it's a bad
> idea.
> Please don't do this in any code that I'll ever have to
> maintain.
No way. You will never get my project code in such a form. I am way
more peculiar about readability and robustness than most of my other
ppl.
The point is, the bpate is just for demonstration to show there is a
*possible* way to find the name even you didn't specify it at first
hand.




-- 

吾輩は猫である。ホームーページはhttp://introo.me。

[toc] | [prev] | [next] | [standalone]


#81710

FromChris Angelico <rosuav@gmail.com>
Date2014-11-29 14:36 +1100
Message-ID<mailman.16423.1417232210.18130.python-list@python.org>
In reply to#81657
On Sat, Nov 29, 2014 at 2:16 PM, Shiyao Ma <i@introo.me> wrote:
> 2014-11-28 13:00 GMT+08:00 Chris Angelico <rosuav@gmail.com>:
>> On Fri, Nov 28, 2014 at 2:04 PM, Shiyao Ma <i@introo.me> wrote:
>>> What if it's in the local namespace of a function or method? IDK, try
>>> to get that thing first.
>>
> Sure enough. I will even avoid using "id" as it's dependent on CPython
> implementation. :)

You can use id() on any object. You are guaranteed to get back an
integer which is both stable and unique among all ids of objects that
exist at the same time as the one you called it on. For as long as the
object continues to exist, that number *will* stay the same. Sometimes
that's all you need; for instance, imagine a simple mail server which
produces logs like this:

[142857] Beginning processing of message
[142857] Parsing headers
[314159] Beginning processing of message
[314159] Parsing headers
[142857] Storing in destination mailbox
[314159] Connecting to destination server
[142857] Finished processing of message
[314159] Message accepted by destination
[271871] Beginning processing of message
[314159] Finished processing of message

You can easily see, from the initial numbers, what log lines are
associated with what messages. (Note that emails have their own IDs,
which could in theory be used, but the id() of an internal dict can be
used even before the email's ID has been read - as you see from the
example, a log entry for "Parsing headers" has to be made prior to
info from the headers being used.) It's not a problem if another
142857 comes up later on; there's a very clear begin and end to the
message, and you're guaranteed that nothing can possibly be
interspersed with a colliding ID.

In other implementations of Python, these numbers might look less
arbitrary (Jython, I believe, allocates them sequentially); but the
code will work just as well on any compliant implementation of the
language, because everything I've said above is a language guarantee,
not a CPython implementation detail.

ChrisA

[toc] | [prev] | [next] | [standalone]


#81711

FromShiyao Ma <i@introo.me>
Date2014-11-29 11:55 +0800
Message-ID<mailman.16424.1417233725.18130.python-list@python.org>
In reply to#81657
2014-11-29 11:36 GMT+08:00 Chris Angelico <rosuav@gmail.com>:
> You can use id() on any object. You are guaranteed to get back an
> integer which is both stable and unique among all ids of objects that
> exist at the same time as the one you called it on. For as long as the
> object continues to exist, that number *will* stay the same. Sometimes
> that's all you need; for instance, imagine a simple mail server which
> produces logs like this:
>
> [142857] Beginning processing of message
> [142857] Parsing headers
> [314159] Beginning processing of message
> [314159] Parsing headers
> [142857] Storing in destination mailbox
> [314159] Connecting to destination server
> [142857] Finished processing of message
> [314159] Message accepted by destination
> [271871] Beginning processing of message
> [314159] Finished processing of message
>
> You can easily see, from the initial numbers, what log lines are
> associated with what messages. (Note that emails have their own IDs,
> which could in theory be used, but the id() of an internal dict can be
> used even before the email's ID has been read - as you see from the
> example, a log entry for "Parsing headers" has to be made prior to
> info from the headers being used.) It's not a problem if another
> 142857 comes up later on; there's a very clear begin and end to the
> message, and you're guaranteed that nothing can possibly be
> interspersed with a colliding ID.
>
> In other implementations of Python, these numbers might look less
> arbitrary (Jython, I believe, allocates them sequentially); but the
> code will work just as well on any compliant implementation of the
> language, because everything I've said above is a language guarantee,
> not a CPython implementation detail.
>
> ChrisA


Thanks. Informed.
The implementation dependent thing is "id(obj)" == virtual mem addr
(which caused my bad feeling).
Apparently have nothing to do with it here, though.

And 'id' is seemingly great to differentiate each obj.



--

吾輩は猫である。ホームーページはhttp://introo.me。

[toc] | [prev] | [standalone]


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


csiph-web