473,396 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

instance name

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
7 2406
"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
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
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
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
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
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
many many thanks to each and everyone who bothered to answer my op.

best regards

macs
Jul 18 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: sinasalek | last post by:
class TTest { function instance_name() { echo "instance name : $instance_name"; } } $test=new TTest(); $text->instance_name();
6
by: Andre Meyer | last post by:
Hi all I have been searching everywhere for this, but have not found a solution, yet. What I need is to create an object that is an instance of a class (NOT a class instance!) of which I only...
6
by: Martin | last post by:
I'd like to be able to get the name of an object instance from within a call to a method of that same object. Is this at all possible? The example below works by passing in the name of the object...
3
by: Adam | last post by:
We have a web site that uses .vb for the web pages and .cs for a class module. We are getting the error in .NET 2.0 and VS 2005 beta 2. It does work with .NET 1.1. When trying to access a page...
3
by: wolfgang.lipp | last post by:
some time after posting my `Linkdict recipe`__ to aspn__ -- basically, a dictionary with run-time delegational lookup, but this is not important here -- i thought gee that would be fun to make...
20
by: Shawnk | last post by:
I would like to get the class INSTANCE name (not type name) of an 'object'. I can get the object (l_obj_ref.GetType()) and then get the (l_obj_typ.Name) for the class name. I there any way of...
2
by: Mesan | last post by:
Hello everyone, Thanks to many useful posts in this newsgroup and others, I've been able to come very close to something I've been wanting to do for a very long time. I've figured out how to...
2
by: Daniel Lipovetsky | last post by:
I would like for an object to "report" to a container object when a new instance is created or deleted. I could have a container object that is called when a new instance is created, as below. ...
1
by: Allen | last post by:
I need a way to add a method to an existing instance, but be as close as possible to normal instance methods. Using 'new' module or such code as 'def addfunc(...): def helper(...) .. setattr(...)'...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.