instance name

M

max(01)*

hi.

is there a way to define a class method which prints the instance name?

e.g.:
.... def myName(self):
.... ????what should i do here????
....
bye

macs
 
A

Andrew Koenig

is there a way to define a class method which prints the instance name?

The term "the instance name" is misleading, because it assumes, without
saying so explicitly, that every instance has a unique name.

In fact, there is no reason that an instance needs to have a name at all, or
that it should have only one.

You gave this example:

instance_1 = class_1()
instance_1.myName()

but what if I did this instead?

class_1().myName()

Or this?

instance_1 = class_1()
instance_2 = instance_1
instance_2.myName()
 
I

Irmen de Jong

max(01)* said:
hi.

is there a way to define a class method which prints the instance name?

e.g.:

... def myName(self):
... ????what should i do here????
...

bye

macs

What should the following do, you think?
????print what????

There is no such thing as "the" instance name.
(a and b both point to the same instance, in my example)

Also: why do you want this? It smells like you're
actually looking for the use of a dict to do your job.

--Irmen
 
M

max(01)*

Andrew said:
The term "the instance name" is misleading, because it assumes, without
saying so explicitly, that every instance has a unique name.

In fact, there is no reason that an instance needs to have a name at all, or
that it should have only one.

You gave this example:

instance_1 = class_1()
instance_1.myName()

but what if I did this instead?

class_1().myName()

Or this?

instance_1 = class_1()
instance_2 = instance_1
instance_2.myName()

excellent points.

i'll get back with proper questions afterwards.

thanks

macs
 
M

max(01)*

Irmen said:
What should the following do, you think?



????print what????

There is no such thing as "the" instance name.
(a and b both point to the same instance, in my example)

Also: why do you want this? It smells like you're
actually looking for the use of a dict to do your job.

right. it seems i need to dive deeper in the language "spirit".

thanks a lot

bye

macs
 
M

Mark Winrock

max(01)* said:
hi.

is there a way to define a class method which prints the instance name?

e.g.:

... def myName(self):
... ????what should i do here????
...

bye

macs

macs,

The object instance doesn't know about the identifier to which you
assigned it (but see my example below using the inspect module).

If you are just trying to identify different instances, you can get a
unique instance identifier from hash() function
hash(instance1)

or
self.__hash__(), or hash(self) from within the object

The default __str__ for a class will normally return something similar
with more information , so that when you
print instance_1
you get a string in the following format:
<module.classname object at hashvalue>


The following is a very, very, very weak example of how you might use
inspect to get the information you want. This is not a robust example at
all, and I imagine it could have many ways it will fail. The init uses
inspect information to dip into the source file and parse out the info
-- like I said, this is not a robust parse.
# ------------------------
import os,sys

class class_1(object):
def __init__(self):
import inspect
self.lineno = inspect.currentframe().f_back.f_lineno
self.filename = inspect.currentframe().f_back.f_code.co_filename
self.initial_instance = None

try:
# i'm not sure if filename is full path or not at this time
f=open(self.filename,'r')
lines=f.readlines()
s = lines[self.lineno-1]
split = s.split()
try:
# find the assignment if possible
i = split.index('=')
self.initial_instance = split[i-1]
except ValueError:
pass
finally:
f.close()

def myName(self):
print 'my initial instance was \'%s\''%(self.initial_instance)

if __name__ == '__main__':

# two straight up examples
instance_1 = class_1()
instance_2 = class_1()

instance_1.myName()
instance_2.myName()

#
class_1().myName()

c = instance_1

c.myName()
# -----------------------------------------------

I got the following results:
my initial instance was 'instance_1'
my initial instance was 'instance_2'
my initial instance was 'None'
my initial instance was 'instance_1'



Best-regards,
Mark
 
B

Bengt Richter

hi.

is there a way to define a class method which prints the instance name?

e.g.:

... def myName(self):
... ????what should i do here????
...
Names in any given name space do not have a 1:1 relationship with class instances.
If you want to give an instance its own name, give it a name when you create it,
or attach a name to the instance. E.g, maybe this will give you an idea:
... def __init__(self, name='(anonymous)'): self.name = name
... def myName(self): return self.name
... 'one'
Did you catch that ;-)

Ok, now without instance name bindings at all, just references from a list:
>>> egoes = [Ego(name) for name in 'one two three'.split()]
>>> egoes
[QUOTE= said:
>>> for ego in egoes: print ego.myName()
[/QUOTE]
...
one
two
three

We don't have to access instance by names like instance_1, if we didn't make
them with bindings like that. E.g., egoes is a name bound to a list of Ego instances,
so we can get at the second (counting from index 0) instance thus, and change it's
name from the outside, since we know it's stored as an attribute called 'name':
>>> egoes[1].name = 'Zeppo'
>>> for ego in egoes: print ego.myName()
...
one
Zeppo
three

HTH

Regards,
Bengt Richter
 
M

max(01)*

many many thanks to each and everyone who bothered to answer my op.

best regards

macs
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,234
Messages
2,571,178
Members
47,810
Latest member
Quinn16P02

Latest Threads

Top