By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,525 Members | 1,634 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,525 IT Pros & Developers. It's quick & easy.

instance name

P: n/a
hi.

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

e.g.:
class class_1: .... def myName(self):
.... ????what should i do here????
.... instance_1 = class_1()
instance_1.myName() 'instance_1'


bye

macs
Jul 18 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
"max(01)*" <ma**@fisso.casa> wrote in message
news:99***********************@news4.tin.it...
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()
Jul 18 '05 #2

P: n/a
max(01)* wrote:
hi.

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

e.g.:
class class_1: ... def myName(self):
... ????what should i do here????
... instance_1 = class_1()
instance_1.myName() 'instance_1'
bye

macs


What should the following do, you think?
a=class_1()
b=a
b is a True b.myName()

????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
Jul 18 '05 #3

P: n/a
Andrew Koenig wrote:
"max(01)*" <ma**@fisso.casa> wrote in message
news:99***********************@news4.tin.it...

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()


excellent points.

i'll get back with proper questions afterwards.

thanks

macs
Jul 18 '05 #4

P: n/a
Irmen de Jong wrote:
max(01)* wrote:
hi.

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

e.g.:

>class class_1:


... def myName(self):
... ????what should i do here????
...
>instance_1 = class_1()
>instance_1.myName()


'instance_1'

bye

macs

What should the following do, you think?

a=class_1()
b=a
b is a
True
b.myName()


????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
Jul 18 '05 #5

P: n/a
max(01)* wrote:
hi.

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

e.g.:
>>> class class_1: ... def myName(self):
... ????what should i do here????
... >>> instance_1 = class_1()
>>> instance_1.myName() 'instance_1' >>>


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
Jul 18 '05 #6

P: n/a
On Sat, 02 Apr 2005 15:48:21 GMT, "max(01)*" <ma**@fisso.casa> wrote:
hi.

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

e.g.:
class class_1:... def myName(self):
... ????what should i do here????
... instance_1 = class_1()
instance_1.myName()'instance_1'

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:
class Ego(object): ... def __init__(self, name='(anonymous)'): self.name = name
... def myName(self): return self.name
... instance_1 = Ego('one')
instance_2 = Ego('two')
instance_1.myName() 'one' instance_2.myName() 'two' instance_2 = instance_1
instance_2.myName() '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 [<__main__.Ego object at 0x02EF1A4C>, <__main__.Ego object at 0x02EF1A6C>, <__main__.Ego object
at 0x02EF1A8C>] for ego in egoes: print ego.myName() ...
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
Jul 18 '05 #7

P: n/a
many many thanks to each and everyone who bothered to answer my op.

best regards

macs
Jul 18 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.