473,587 Members | 2,508 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.myNa me() 'instance_1'


bye

macs
Jul 18 '05 #1
7 2413
"max(01)*" <ma**@fisso.cas a> 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.myNa me()

but what if I did this instead?

class_1().myNam e()

Or this?

instance_1 = class_1()
instance_2 = instance_1
instance_2.myNa me()
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.myNa me() '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.cas a> 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.myNa me()

but what if I did this instead?

class_1().myNam e()

Or this?

instance_1 = class_1()
instance_2 = instance_1
instance_2.myNa me()


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.myNa me() '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.classna me 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.current frame().f_back. f_lineno
self.filename = inspect.current frame().f_back. f_code.co_filen ame
self.initial_in stance = None

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

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

if __name__ == '__main__':

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

instance_1.myNa me()
instance_2.myNa me()

#
class_1().myNam e()

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.cas a> 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.myNa me()'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='(anonymou s)'): self.name = name
... def myName(self): return self.name
... instance_1 = Ego('one')
instance_2 = Ego('two')
instance_1.myNa me() 'one' instance_2.myNa me() 'two' instance_2 = instance_1
instance_2.myNa me() '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
4954
by: sinasalek | last post by:
class TTest { function instance_name() { echo "instance name : $instance_name"; } } $test=new TTest(); $text->instance_name();
6
7742
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 know the name as a string. This what I tried: class A:
6
22505
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 instance (in this case 'myDog'). Of course it would be better if I could somehow know from within write() that the name of the object instance was...
3
4205
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 that needs the class module I get an error on web site: Object reference not set to an instance of an object Here is where the error is:
3
1492
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 such a customized dictionary thingie an instance dictionary, and get some custom namespace behavior out of that. ... __:...
20
27749
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 getting 'l_Fred_my_ins' out of the following. .... My_cls l_Fred_my_ins = new My_cls();
2
5492
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 create a new custom protocol handler in Windows to handle locations like "myProtocol:", which lets me have a shortcut pointing to...
2
1984
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. class AnyObject: pass class Container: links = def add(self,other):
1
1729
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(...)' causes a cyclic reference which requires using 'gc.collect' to release the object. Also 'new' is deprecated. I also made a helper that uses...
0
7852
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7974
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8221
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6629
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5395
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3845
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3882
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1455
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1192
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.