Method to give name of class instance? 
Author Message
 Method to give name of class instance?

Hi Everyone,

I've been enjoying learning python very much, but have hit a little
snag.  I want a way to find out the name of a class instance from a
method.  eg.

Quote:
>>>classinstance=MyClass()
>>>classinstance.myname()

'classinstance'

Is there a way to do this?

Thanks,
-Tim



Fri, 17 Oct 2003 08:22:53 GMT  
 Method to give name of class instance?


Quote:
>Hi Everyone,

>I've been enjoying learning Python very much, but have hit a little
>snag.  I want a way to find out the name of a class instance from a
>method.  eg.

>>>>classinstance=MyClass()
>>>>classinstance.myname()
>'classinstance'

>Is there a way to do this?

If you want it to have a name, you're going to have to give it one yourself...

class MyClass:
    def __init__(self, myname):
        self.myname = myname

Quote:
>>>classinstance=MyClass('classinstance')
>>>classinstance.myname()
>>>'classinstance'

Daniel Klein
Portland, OR USA


Fri, 17 Oct 2003 09:11:12 GMT  
 Method to give name of class instance?

Quote:
> Hi Everyone,

> I've been enjoying learning Python very much, but have hit a little
> snag.  I want a way to find out the name of a class instance from a
> method.  eg.

> >>>classinstance=MyClass()
> >>>classinstance.myname()
> 'classinstance'

> Is there a way to do this?

No, instances don't have names because they may have any number
of names bound to them, even none at all.

Example 1:

    a = MyClass()
    b = a

Here, both a and b refer to the same instance.

Example 2:

    a = [MyClass()]

Here, no name refers to the instance.

--
Any opinions expressed are my own and not necessarily those of Roundpoint.



Fri, 17 Oct 2003 09:44:53 GMT  
 Method to give name of class instance?

Quote:
> Hi Everyone,

> I've been enjoying learning Python very much, but have hit a little
> snag.  I want a way to find out the name of a class instance from a
> method.  eg.

> >>>classinstance=MyClass()
> >>>classinstance.myname()
> 'classinstance'

> Is there a way to do this?

No, instances don't have names because they may have any number
of names bound to them, even none at all.

Example 1:

    a = MyClass()
    b = a

Here, both a and b refer to the same instance.

Example 2:

    a = [MyClass()]

Here, no name refers to the instance.

--
Any opinions expressed are my own and not necessarily those of Roundpoint.



Fri, 17 Oct 2003 09:45:06 GMT  
 Method to give name of class instance?


Quote:
> Hi Everyone,

> I've been enjoying learning Python very much, but have hit a little
> snag.  I want a way to find out the name of a class instance from a
> method.  eg.

> >>>classinstance=MyClass()
> >>>classinstance.myname()
> 'classinstance'

> Is there a way to do this?

Python's Frequently Asked Questions (FAQ) are a very useful resource.
You are likely to find reasonable answers to most questions which ARE
frequently asked, such as this one.  Specifically, see
    http://www.python.org/doc/FAQ.html#4.97

What that FAQ doesn't say is that you CAN easily write functions
that will look for (all occurrences/first occurrence) of a certain
value (AKA object) in a set of variables (which is, after all, just
a dictionary).  For example:

def oneName(object, vars):
    for name, value in vars.items():
        if value is object: return name
    return None

to be called as oneName(classinstance, vars()) if you need "just
any name" (be it local or global), oneName(classinstance, locals())
if you need specifically a local variable name for the instance,
oneName(classinstance, globals()) if you need a global variable
name in the current module, oneName(classinstance, dir(amod)) if
you need it in module object amod, and so on.

It *IS* possible to play "black magic" tricks so the function
will be able to use the *caller's* locals or globals by default,
but "black magic as a way of life" is *NOT* a programming
approach that can be recommended (either in Python, or in any
other language)...

Alex



Fri, 17 Oct 2003 15:48:35 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Singleton methods and instance methods with same name?

2. accessing a instance method from a class method

3. Class methods vs Instance methods

4. Class method vs instance method???

5. Class method vs instance method???

6. same method as class and instance methods?

7. Class Methods (as opposed to Instance Methods)

8. Object Tcl question - Class method and Instance method

9. Classes - instance/class, methods/members, inheritance and best p ractices

10. Classes - instance/class, methods/members, inheritance and be st practices

11. How to call a class method from a string representing a class name

12. Q.: Retrieve all instance for any given class?

 

 
Powered by phpBB® Forum Software