Re: Object Reference question

2009-09-01 Thread Bruno Desthuilliers

Ethan Furman a écrit :
(snip)

The best answer I can give is that you do not want to use 'name' to 
reference the object itself, but only for printing/debugging purposes. 


Which is what the OP stated !-)

'name' is just a label for your object, and not necessarily the only 
label;  that particular label may also be lost... Consider:


(snip example)

As you can see, just because you have saved the original name does not 
gaurantee that same name will always reference that same object, or any 
object.



FWIW, the __name__ attributes of functions, classes and modules works 
just the same...

--
http://mail.python.org/mailman/listinfo/python-list


Re: Object Reference question

2009-08-31 Thread Ethan Furman

josef wrote:

On Aug 27, 1:35 pm, Ethan Furman et...@stoneleaf.us wrote:


josef wrote:


Thanks to everyone who responded.



I will be going with some sort of a = MyClass(name = 'a') format. It's
the Python way.



For me, it was very hard to accept that EVERYTHING is an object
reference. And that there are no object reference names, just string
entries in dictionaries. But I think it all makes sense now.



Thanks again,



Josef


My apologies if I missed it, but what *exactly* are you planning on
doing with your 'name' attribute?  From the posts I've seen so far, I
think you are only setting yourself up for failure.

~Ethan~



I'm going to use it for printing purposes. dk = MyClass(name='dk')
When I need a name dk.name. There will only ever be one dk defined.

Does that read like I'm setting myself up for failure?


I was hoping someone with more expertise than myself would answer that. 
 :)  Oh well.


The best answer I can give is that you do not want to use 'name' to 
reference the object itself, but only for printing/debugging purposes. 
'name' is just a label for your object, and not necessarily the only 
label;  that particular label may also be lost... Consider:


In [5]: class MyClass(object):
   ...: def __init__(self, name):
   ...: self.name = name
   ...: def __repr__(self):
   ...: return MyClass(name='%s') % self.name
   ...:

In [6]: dk = MyClass(name='dk')

In [7]: dk
Out[7]: MyClass(name='dk')

In [8]: se = dk

In [9]: del dk

In [10]: se
Out[10]: MyClass(name='dk')

In [11]: dk
---
NameError Traceback (most recent call last)

C:\pythonlib\ipython console in module()

NameError: name 'dk' is not defined

As you can see, just because you have saved the original name does not 
gaurantee that same name will always reference that same object, or any 
object.


Hope this helps!

~Ethan~
--
http://mail.python.org/mailman/listinfo/python-list


Re: Object Reference question

2009-08-28 Thread josef
On Aug 27, 1:35 pm, Ethan Furman et...@stoneleaf.us wrote:
 josef wrote:
  Thanks to everyone who responded.

  I will be going with some sort of a = MyClass(name = 'a') format. It's
  the Python way.

  For me, it was very hard to accept that EVERYTHING is an object
  reference. And that there are no object reference names, just string
  entries in dictionaries. But I think it all makes sense now.

  Thanks again,

  Josef

 My apologies if I missed it, but what *exactly* are you planning on
 doing with your 'name' attribute?  From the posts I've seen so far, I
 think you are only setting yourself up for failure.

 ~Ethan~

I'm going to use it for printing purposes. dk = MyClass(name='dk')
When I need a name dk.name. There will only ever be one dk defined.

Does that read like I'm setting myself up for failure?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Object Reference question

2009-08-27 Thread josef
Thanks to everyone who responded.

I will be going with some sort of a = MyClass(name = 'a') format. It's
the Python way.

For me, it was very hard to accept that EVERYTHING is an object
reference. And that there are no object reference names, just string
entries in dictionaries. But I think it all makes sense now.

Thanks again,

Josef


On Aug 21, 1:07 am, josef jos...@gmail.com wrote:
 To begin, I'm new with python. I've read a few discussions about
 object references and I think I understand them.

 To be clear, Python uses a Pass By Object Reference model.
 x = 1
 x becomes the object reference, while an object is created with the
 type 'int', value 1, and identifier (id(x)). Doing this with a class,
 x = myclass(), does the same thing, but with more or less object
 attributes. Every object has a type and an identifier (id()),
 according to the Python Language Reference for 2.6.2 section 3.1.

 x in both cases is the object reference. I would like to use the
 object to refer to the object reference. If I have a gross
 misunderstanding, please correct me.

 The following is what I would like to do:
 I have a list of class instances dk = [ a, b, c, d ], where a, b, c, d
 is an object reference. Entering dk gives me the object: [MyClass0
 instance at 0x, MyClass1 instance at 0x0008, MyClass2 instance at
 0x0010 ... ]

 I need the object reference name (a,b,c,d) from dk to use as input for
 a file. Where do I find the memory location of the object reference
 and the object reference name memory location? I am unconcerned with
 the fact that the memory location will change the next time I run a
 python session. I will be using the object reference name for
 processing right away.

 My main focus of this post is: How do I find and use object reference
 memory locations?

 Thoughts?
 Thanks,

 Josef

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Object Reference question

2009-08-27 Thread Ethan Furman

josef wrote:

Thanks to everyone who responded.

I will be going with some sort of a = MyClass(name = 'a') format. It's
the Python way.

For me, it was very hard to accept that EVERYTHING is an object
reference. And that there are no object reference names, just string
entries in dictionaries. But I think it all makes sense now.

Thanks again,

Josef


My apologies if I missed it, but what *exactly* are you planning on 
doing with your 'name' attribute?  From the posts I've seen so far, I 
think you are only setting yourself up for failure.


~Ethan~

--
http://mail.python.org/mailman/listinfo/python-list


Re: Object Reference question

2009-08-26 Thread Hendrik van Rooyen
On Tuesday 25 August 2009 21:32:09 Aahz wrote:
 In article mailman.164.1250837108.2854.python-l...@python.org,

 Hendrik van Rooyen  hend...@microcorp.co.za wrote:
 On Friday 21 August 2009 08:07:18 josef wrote:
  My main focus of this post is: How do I find and use object reference
  memory locations?
 
  a = [1,2,3,4]
  id(a)
 
 8347088

 Of course, that doesn't actually allow you to do anything...

Well - if the OP is the sort of person who likes juggling with running 
chainsaws, then he can look up a thread I started about a thing I called 
a can, which enabled you to get the object back from a string 
representation of the ID.  I did not want to open that can of worms again, 
and I thought that answering half a question was better than nothing...

*weg*  - Hendrik

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Object Reference question

2009-08-25 Thread Aahz
In article mailman.164.1250837108.2854.python-l...@python.org,
Hendrik van Rooyen  hend...@microcorp.co.za wrote:
On Friday 21 August 2009 08:07:18 josef wrote:

 My main focus of this post is: How do I find and use object reference
 memory locations?

 a = [1,2,3,4]
 id(a)
8347088
  

Of course, that doesn't actually allow you to do anything...
-- 
Aahz (a...@pythoncraft.com)   * http://www.pythoncraft.com/

I support family values -- Addams family values --www.nancybuttons.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Object Reference question

2009-08-24 Thread Bruno Desthuilliers

josef a écrit :

(snip)
 I think that something like a = MyClass0(name =

'a', ...) is a bit redundant. Are definitions treated the same way?
How would one print or pass function names?


In Python, classes and functions are objects too. The class and def 
statements are mostly syntactic sugar that *both* instanciate the (resp) 
class of function objet *and* bind it in the local namespace. FWIW, 
while class and function objects do (usually) have a __name__ attribute 
(usually the one used in the class or def statement...), this doesn't 
mean they're still bound to this name, nor that they are not bound to 
any other name:


br...@bruno:~$ python
Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41)
[GCC 4.3.3] on linux2
Type help, copyright, credits or license for more information.
pythonrc start
pythonrc done
 def foo(): print function foo ???
...
 foo
function foo at 0x8563e9c
 foo.__name__
'foo'
 bar = foo
 bar.__name__
'foo'
 bar()
function foo ???
 import sys
 foo = lambda: sys.stdout.write(pick a boo !\n)
 foo.__name__
'lambda'
 foo()
pick a boo !
 funcs = [foo, bar]
 del foo
 funcs[0]
function lambda at 0x8563ed4
 funcs[0]()
pick a boo !
 funcs[1]
function foo at 0x8563e9c
 funcs[1]()
function foo ???
 funcs[1].__name__
'foo'
 del bar
 funcs[1]()
function foo ???
 funcs[1].__name__ = yadda
 funcs[1]()
function foo ???
 funcs[1].__name__
'yadda'


HTH
--
http://mail.python.org/mailman/listinfo/python-list


Re: Object Reference question

2009-08-21 Thread Miles Kaufmann

On Aug 20, 2009, at 11:07 PM, josef wrote:


To begin, I'm new with python. I've read a few discussions about
object references and I think I understand them.

To be clear, Python uses a Pass By Object Reference model.
x = 1
x becomes the object reference, while an object is created with the
type 'int', value 1, and identifier (id(x)). Doing this with a class,
x = myclass(), does the same thing, but with more or less object
attributes. Every object has a type and an identifier (id()),
according to the Python Language Reference for 2.6.2 section 3.1.

x in both cases is the object reference. I would like to use the
object to refer to the object reference.


Stop right there.  'x' is not *the* object reference.  It is *an*  
object reference (or in my preferred terminology, a label).  Suppose  
you do:


x = myclass()
y = x

The labels 'x' and 'y' both refer to the same object with equal  
precedence.  There is no mapping from object back to label; it is a  
one-way pointer.  Also importantly, labels themselves are not objects,  
and cannot be accessed or referred to.


(This is a slight oversimplification; thanks to Python's reflection  
and introspection capabilities, it is possible to access labels to  
some extent, and in some limited situations it is possible to use  
stack inspection to obtain a label for an object.  But this is hackish  
and error-prone, and should never be used when a more Pythonic method  
is available.)



The following is what I would like to do:
I have a list of class instances dk = [ a, b, c, d ], where a, b, c, d
is an object reference. Entering dk gives me the object: [MyClass0
instance at 0x, MyClass1 instance at 0x0008, MyClass2 instance at
0x0010 ... ]

I need the object reference name (a,b,c,d) from dk to use as input for
a file.


It sounds like you should either be storing that name as an attribute  
of the object, or using a dictionary ({'a': a, 'b': b, ...}).


-Miles

--
http://mail.python.org/mailman/listinfo/python-list


Re: Object Reference question

2009-08-21 Thread Chris Rebert
On Thu, Aug 20, 2009 at 11:34 PM, Miles Kaufmannmile...@umich.edu wrote:
 On Aug 20, 2009, at 11:07 PM, josef wrote:
snip
 The following is what I would like to do:
 I have a list of class instances dk = [ a, b, c, d ], where a, b, c, d
 is an object reference. Entering dk gives me the object: [MyClass0
 instance at 0x, MyClass1 instance at 0x0008, MyClass2 instance at
 0x0010 ... ]

 I need the object reference name (a,b,c,d) from dk to use as input for
 a file.

 It sounds like you should either be storing that name as an attribute of the
 object, or using a dictionary ({'a': a, 'b': b, ...}).

Shorter way to produce the same dictionary:
dict(a=a, b=b, ...)

Cheers,
Chris
--
http://blog.rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Object Reference question

2009-08-21 Thread Hendrik van Rooyen
On Friday 21 August 2009 08:07:18 josef wrote:

 My main focus of this post is: How do I find and use object reference
 memory locations?

 a = [1,2,3,4]
 id(a)
8347088
  

- Hendrik
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Object Reference question

2009-08-21 Thread josef
On Aug 21, 1:34 am, Miles Kaufmann mile...@umich.edu wrote:
 On Aug 20, 2009, at 11:07 PM, josef wrote:

  To begin, I'm new with python. I've read a few discussions about
  object references and I think I understand them.

  To be clear, Python uses a Pass By Object Reference model.
  x = 1
  x becomes the object reference, while an object is created with the
  type 'int', value 1, and identifier (id(x)). Doing this with a class,
  x = myclass(), does the same thing, but with more or less object
  attributes. Every object has a type and an identifier (id()),
  according to the Python Language Reference for 2.6.2 section 3.1.

  x in both cases is the object reference. I would like to use the
  object to refer to the object reference.

 Stop right there.  'x' is not *the* object reference.  It is *an*  
 object reference (or in my preferred terminology, a label).  Suppose  
 you do:

 x = myclass()
 y = x

It would not make sense to do that in the context of the software I am
writing. The documentation will specifically state not to do that. If
the user does do that, then the user will be disappointed and possibly
angry.


 The labels 'x' and 'y' both refer to the same object with equal  
 precedence.  There is no mapping from object back to label; it is a  
 one-way pointer.  Also importantly, labels themselves are not objects,  
 and cannot be accessed or referred to.

I would just like to store the name of the one way pointer.


 (This is a slight oversimplification; thanks to Python's reflection  
 and introspection capabilities, it is possible to access labels to  
 some extent, and in some limited situations it is possible to use  
 stack inspection to obtain a label for an object.  But this is hackish  
 and error-prone, and should never be used when a more Pythonic method  
 is available.)

Hackish is fine. How error-prone is this method?

  The following is what I would like to do:
  I have a list of class instances dk = [ a, b, c, d ], where a, b, c, d
  is an object reference. Entering dk gives me the object: [MyClass0
  instance at 0x, MyClass1 instance at 0x0008, MyClass2 instance at
  0x0010 ... ]

  I need the object reference name (a,b,c,d) from dk to use as input for
  a file.

 It sounds like you should either be storing that name as an attribute  
 of the object, or using a dictionary ({'a': a, 'b': b, ...}).

That solution was mentioned in some of the discussions I read, but I
would like to stay away from something like: a = MyClass
(name='a', ...). Is it possible to assign an object reference name in
a class __init__ defintion?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Object Reference question

2009-08-21 Thread Dave Angel

josef wrote:

To begin, I'm new with python. I've read a few discussions about
object references and I think I understand them.

To be clear, Python uses a Pass By Object Reference model.
x = 1
x becomes the object reference, while an object is created with the
type 'int', value 1, and identifier (id(x)). Doing this with a class,
x = myclass(), does the same thing, but with more or less object
attributes. Every object has a type and an identifier (id()),
according to the Python Language Reference for 2.6.2 section 3.1.

x in both cases is the object reference. I would like to use the
object to refer to the object reference. If I have a gross
misunderstanding, please correct me.

The following is what I would like to do:
I have a list of class instances dk = [ a, b, c, d ], where a, b, c, d
is an object reference. Entering dk gives me the object: [MyClass0
instance at 0x, MyClass1 instance at 0x0008, MyClass2 instance at
0x0010 ... ]

I need the object reference name (a,b,c,d) from dk to use as input for
a file. Where do I find the memory location of the object reference
and the object reference name memory location? I am unconcerned with
the fact that the memory location will change the next time I run a
python session. I will be using the object reference name for
processing right away.

My main focus of this post is: How do I find and use object reference
memory locations?

Thoughts?
Thanks,

Josef

  
There was a similar query here within the last couple of months, and 
lots of interesting discussion.  But I never saw a use case convincing 
enough for me to want to remember how the various suggestions worked.  
Just how are you planning to use this?  Are you planning to write a 
debugger?


Or are you trying to keep mnemonic names for all instances of a 
particular class?  Is this for a particular program's use, or are you 
trying to create a library to be used to reverse engineer some software 
you con't control?


Several of your phrasings imply you don't understand Python yet.

memory location - invisible to python use.  And although id() will 
give you a hash-code that's actually a memory address, there's no direct 
way to use it.  And names (attributes) don't necessarily have an address.
the object reference name (a,b,c,d) from dk  What is this?  There's 
nothing that even conceptually looks like that when you assign   dk = 
[a, b, c, d]



A given object may have one to many references, and some of these may 
have names.  If you constrain those names to be in a particular context, 
it may be possible to search for which name(s) currently happen(s) to 
point to the given object.  For example, if you have the following at 
top level in a module:


a = MyClass0()
b = MyClass1()
c = MyClass2()
dk = [a, b, c]

then, given the  id() of  dk[2], you could search the particular modules 
global name dictionary, and find c.  But for the following fragment, you 
could not:


a = MyClass0()
b = MyClass1()
c = MyClass2()
dk = [a, b, c]
c = 42

dk remains the same, but the dk[2] item no longer has any name 
referencing it.


At any given instant of time, most objects in a typical program have no 
name associated with them.  Many of them never did have a name.  What 
would you want if dk had been created as:


dk = [MyClass0(), MyClass1(), MyClass2()]

or if a, b, and/or c were local variables in a function that's long 
since quit running, or that has run many times, each time creating new 
objects?


DaveA

--
http://mail.python.org/mailman/listinfo/python-list


Re: Object Reference question

2009-08-21 Thread Bruno Desthuilliers

josef a écrit :

To begin, I'm new with python. I've read a few discussions about
object references and I think I understand them.

To be clear, Python uses a Pass By Object Reference model.
x = 1
x becomes the object reference, while an object is created with the
type 'int', value 1, and identifier (id(x)). Doing this with a class,
x = myclass(), does the same thing, but with more or less object
attributes. Every object has a type and an identifier (id()),
according to the Python Language Reference for 2.6.2 section 3.1.

x in both cases is the object reference. 


Nope. It's *a* reference to the object - or, more exactly, a key in a 
mapping (the current namespace), which is associatied with a reference 
to the object. You can translate:


   x = 1

to:

  current_namespace['x'] = int(1)



I would like to use the
object to refer to the object reference. If I have a gross
misunderstanding, please correct me.

The following is what I would like to do:
I have a list of class instances dk = [ a, b, c, d ], where a, b, c, d
is an object reference. Entering dk gives me the object: [MyClass0
instance at 0x, MyClass1 instance at 0x0008, MyClass2 instance at
0x0010 ... ]

I need the object reference name (a,b,c,d) from dk to use as input for
a file.


???

Could you elaborate, please ?


 Where do I find the memory location of the object reference
and the object reference name memory location? 


short answer : you don't. Python is a high level language, 'memory 
location' is an implementation detail (and highly 
implementation-dependant), and *not* exposed (at least not in any usable 
way).



I am unconcerned with
the fact that the memory location will change the next time I run a
python session. I will be using the object reference name for
processing right away.

My main focus of this post is: How do I find and use object reference
memory locations?

Thoughts?


Yes : please explain the problem you're trying to solve. I mean, the 
*real* problem - what you want to achieve -, not what you think is the 
solution !-)

--
http://mail.python.org/mailman/listinfo/python-list


Re: Object Reference question

2009-08-21 Thread Ben Finney
josef jos...@gmail.com writes:

 To be clear, Python uses a Pass By Object Reference model.

Yes. (I'm glad this concept has propagated to newcomers so well :-)

 x = 1
 x becomes the object reference

It becomes *a* reference to that object, independent of any other
references to that same object.

 while an object is created with the type 'int', value 1, and
 identifier (id(x)).

Not really “while”. The object creation happens first, then the
assignment statement binds a reference to that object.

 Doing this with a class, x = myclass(), does the same thing, but with
 more or less object attributes. Every object has a type and an
 identifier (id()), according to the Python Language Reference for
 2.6.2 section 3.1.

Any expression can be on the right side of the assignment operator. The
expression will evaluate to some object, which the assignment will then
bind to the reference on the left side of the assignment operator.

 x in both cases is the object reference.

It is *an* object reference; that is, it's an identifier which refers to
an object. There's nothing about that identifier that makes it “the (one
and only) object reference”.

 I would like to use the object to refer to the object reference. If I
 have a gross misunderstanding, please correct me.

Yes, it's a simple misunderstanding: objects do not, in general, know
any of the references there may be to them.

 The following is what I would like to do: I have a list of class
 instances dk = [ a, b, c, d ], where a, b, c, d is an object
 reference.

Note that, after that list is created, each item in that list is *also*
a reference to the corresponding object. That is, ‘a’ is a reference to
an object, and ‘dk[0]’ is a *different* reference to the *same* object.
The object has no knowledge about those references.

 Entering dk gives me the object: [MyClass0 instance at 0x,
 MyClass1 instance at 0x0008, MyClass2 instance at 0x0010 ... ]

This is a hint that, when asked for a string representation, each of the
objects in that list can say little more than that they are of a
particular type, and are located at a particular memory address. They do
not know any of the references to themselves.

 I need the object reference name (a,b,c,d) from dk to use as input for
 a file.

You'll have to track that yourself.

A good way to keep track of name-to-object mappings is with Python's
built-in mapping type, ‘dict’::

dk = {'a': a, 'b': b, 'c': c, 'd': d}

(There are more efficient ways to create a dictionary without such
repetition, of course, but this is more illustrative of the point.)

You can then get a list (assembled in arbitrary sequence) of just the
keys, or just the values, or the key-value pairs, from the dict with its
‘keys’, ‘values’, and ‘items’ methods respectively::

 dk = {'a': a, 'b': b, 'c': c, 'd': d}
 dk.keys()
['a', 'c', 'd', 'b']
 dk.values()
[MyClass instance at 0x3462, MyClass instance at 0x2983, MyClass 
instance at 0x3717, MyClass instance at 0x3384]
 dk.items()
[('b', MyClass instance at 0x2983), ('c', MyClass instance at 0x3462), 
('a',  MyClass instance at 0x3384), ('d',  MyClass instance at 0x3717)]

Each of these is even better used as the iterable for a ‘for’ loop::

 for (key, value) in dk.items():
... print Here is item named, key
... print value

 My main focus of this post is: How do I find and use object reference
 memory locations?

You don't. Use the references in your code, forget about the memory
addresses, and remember that container objects themselves contain
references, so use them for organising your objects.

-- 
 \ “Demagogue: One who preaches doctrines he knows to be untrue to |
  `\ men he knows to be idiots.” —Henry L. Mencken |
_o__)  |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Object Reference question

2009-08-21 Thread josef
On Aug 21, 4:26 am, Ben Finney ben+pyt...@benfinney.id.au wrote:
 josef jos...@gmail.com writes:
  To be clear, Python uses a Pass By Object Reference model.

 Yes. (I'm glad this concept has propagated to newcomers so well :-)

I found one really good discussion on python semantics versus other
languages. It gave me this gem of a quote:

When I turn on the TV and see Chuck Norris, though, I know it's only
a reference to Chuck Norris, or I would be blinded.  The only case he
needs is Pass By Roundhouse Kick. -Chuckk


  x = 1
  x becomes the object reference

 It becomes *a* reference to that object, independent of any other
 references to that same object.

  while an object is created with the type 'int', value 1, and
  identifier (id(x)).

 Not really “while”. The object creation happens first, then the
 assignment statement binds a reference to that object.

  Doing this with a class, x = myclass(), does the same thing, but with
  more or less object attributes. Every object has a type and an
  identifier (id()), according to the Python Language Reference for
  2.6.2 section 3.1.

 Any expression can be on the right side of the assignment operator. The
 expression will evaluate to some object, which the assignment will then
 bind to the reference on the left side of the assignment operator.

  x in both cases is the object reference.

 It is *an* object reference; that is, it's an identifier which refers to
 an object. There's nothing about that identifier that makes it “the (one
 and only) object reference”.

  I would like to use the object to refer to the object reference. If I
  have a gross misunderstanding, please correct me.

 Yes, it's a simple misunderstanding: objects do not, in general, know
 any of the references there may be to them.

  The following is what I would like to do: I have a list of class
  instances dk = [ a, b, c, d ], where a, b, c, d is an object
  reference.

 Note that, after that list is created, each item in that list is *also*
 a reference to the corresponding object. That is, ‘a’ is a reference to
 an object, and ‘dk[0]’ is a *different* reference to the *same* object.
 The object has no knowledge about those references.

This is surprising. My initial thought is that dk[0] hold the object
reference 'a,' but that wouldn't be true pass by object reference.
When defining the object reference dk[0], python takes the object
reference 'a,' finds the object MyClass0(), and then assigns the
object identity to dk[0]? Or something close to that.


  Entering dk gives me the object: [MyClass0 instance at 0x,
  MyClass1 instance at 0x0008, MyClass2 instance at 0x0010 ... ]

 This is a hint that, when asked for a string representation, each of the
 objects in that list can say little more than that they are of a
 particular type, and are located at a particular memory address. They do
 not know any of the references to themselves.

  I need the object reference name (a,b,c,d) from dk to use as input for
  a file.

 You'll have to track that yourself.

I'm a bit shocked that there isn't a method for catching object
reference names. I think that something like a = MyClass0(name =
'a', ...) is a bit redundant. Are definitions treated the same way?
How would one print or pass function names?


 A good way to keep track of name-to-object mappings is with Python's
 built-in mapping type, ‘dict’::

     dk = {'a': a, 'b': b, 'c': c, 'd': d}

 (There are more efficient ways to create a dictionary without such
 repetition, of course, but this is more illustrative of the point.)

 You can then get a list (assembled in arbitrary sequence) of just the
 keys, or just the values, or the key-value pairs, from the dict with its
 ‘keys’, ‘values’, and ‘items’ methods respectively::

      dk = {'a': a, 'b': b, 'c': c, 'd': d}
      dk.keys()
     ['a', 'c', 'd', 'b']
      dk.values()
     [MyClass instance at 0x3462, MyClass instance at 0x2983, MyClass 
 instance at 0x3717, MyClass instance at 0x3384]
      dk.items()
     [('b', MyClass instance at 0x2983), ('c', MyClass instance at 
 0x3462), ('a',  MyClass instance at 0x3384), ('d',  MyClass instance at 
 0x3717)]

 Each of these is even better used as the iterable for a ‘for’ loop::

      for (key, value) in dk.items():
     ...     print Here is item named, key
     ...     print value

I think I'll just add a 'name' to the classes' init defintion.


  My main focus of this post is: How do I find and use object reference
  memory locations?

 You don't. Use the references in your code, forget about the memory
 addresses, and remember that container objects themselves contain
 references, so use them for organising your objects.

 --
  \     “Demagogue: One who preaches doctrines he knows to be untrue to |
   `\                     men he knows to be idiots.” —Henry L. Mencken |
 _o__)                                                                  |
 Ben Finney

Thanks,

Josef
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Object Reference question

2009-08-21 Thread Ben Finney
josef jos...@gmail.com writes:

 On Aug 21, 4:26 am, Ben Finney ben+pyt...@benfinney.id.au wrote:
  Note that, after that list is created, each item in that list is
  *also* a reference to the corresponding object. That is, ‘a’ is a
  reference to an object, and ‘dk[0]’ is a *different* reference to
  the *same* object. The object has no knowledge about those
  references.

 This is surprising.

Perhaps so, depending on your initial assumptions. But it's quite
consistent: every time you use an object, you do so via some kind of
reference to that object. Those references are the way you get at the
same object again later.

 My initial thought is that dk[0] hold the object reference 'a,' but
 that wouldn't be true pass by object reference.

Right. It stores an entirely *separate* reference to that object. The
reference ‘dk[0]’, a list item, and the reference ‘a’, a name, both
refer to the same object. Neither of those references knows anything
about the other.

 When defining the object reference dk[0]

Not “defining”. You're binding (“assigning”, if you like) a reference.

 python takes the object reference 'a,'

More accurately, it takes the object referred to by ‘a’. (Also, please
don't put the comma inside the quotes; it's syntactically significant,
and changes the meaning of what you're writing in code.)

 finds the object MyClass0()

No. ‘MyClass0()’ is syntax for “call MyClass0 and get its return value”.
The return value will be a new instance of the class — a *new* object
every time you use that syntax.

Rather, the object referred to by ‘a’ will be the result of evaluating
the identifier ‘a’. As a shortcut for discussion, you can talk about
“the object ‘a’”, but remember that the only things you're using in the
syntax of your Python code is object *references*, be they identifiers
(names) or some other kind of reference.

 and then assigns the object identity to dk[0]? Or something close to
 that.

You'd do well to read URL:http://effbot.org/zone/python-objects.htm
for a good, simple coverage of the Python object model.

 I'm a bit shocked that there isn't a method for catching object
 reference names.

Don't be; there is no such thing as an “object reference name”.

A name is an object reference. So is every other way of getting at a
Python object in your code.

 I think that something like a = MyClass0(name = 'a', ...) is a bit
 redundant.

It usually is, yes. Why do you think you need it? The natural way to
handle objects and names together in Python is with a mapping; a ‘dict’
instance.

 Are definitions treated the same way? How would one print or pass
 function names?

Functions are objects like any other, but since they're more likely to
want to know their own name, the name is stored as an attribute::

 def frobnicate_nodule(spam):
...  Frobnicate the spam nodule. 
... 
 type(frobnicate_nodule)
type 'function'
 frobnicate_nodule.__name__
'frobnicate_nodule'

The fact that the function name is stored in one of those funky
double-underscore names is a big clue that the attribute is special (in
this case, because it gets assigned automatically by the Python
interpreter).

That's not the case for most types, though.

 I think I'll just add a 'name' to the classes' init defintion.

What is the larger problem you're trying to solve, and why do you think
it will be helped by instances knowing a name for themselves?

(Note: an object can never know *all* names for itself, let alone all
the other references to itself; and you must write your program in the
knowledge that no reference to an object has any intrinsic special
status against any of the other references to that object.)

-- 
 \“I spent a lot of money on wine and women, and like a fool I |
  `\ squandered the rest.” —Benny Hill |
_o__)  |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list