Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Getting the name of an assignment

Reply
Thread Tools

Getting the name of an assignment

 
 
Adam Atlas
Guest
Posts: n/a
 
      12-23-2006
Is it possible for an object, in its __init__ method, to find out if it
is being assigned to a variable, and if so, what that variable's name
is? I can think of some potentially ugly ways of finding out using
sys._getframe, but if possible I'd prefer something less exotic.
(Basically I have a class whose instances, upon being created, need a
'name' property, and if it's being assigned to a variable immediately,
that variable's name would be the best value of 'name'; to make the
code cleaner and less redundant, it would be best if it knew its own
name upon creation, just like functions and classes do, without the
code having to pass it its own name as a string.)

 
Reply With Quote
 
 
 
 
=?ISO-8859-1?Q?BJ=F6rn_Lindqvist?=
Guest
Posts: n/a
 
      12-23-2006
On 23 Dec 2006 14:38:19 -0800, Adam Atlas <(E-Mail Removed)> wrote:
> Is it possible for an object, in its __init__ method, to find out if it
> is being assigned to a variable, and if so, what that variable's name
> is? I can think of some potentially ugly ways of finding out using
> sys._getframe, but if possible I'd prefer something less exotic.
> (Basically I have a class whose instances, upon being created, need a
> 'name' property, and if it's being assigned to a variable immediately,
> that variable's name would be the best value of 'name'; to make the
> code cleaner and less redundant, it would be best if it knew its own
> name upon creation, just like functions and classes do, without the
> code having to pass it its own name as a string.)


I guess you mean something like this:

>>> olle = Person()
>>> olle.name

"olle"

Instead of:

>>> olle = Person("olle")
>>> olle.name

"olle"

It is not possible without ugly hacks. What you could use instead is
some kind of registry approach:

reg = {}
class Person:
def __init__(self, name):
self.name = name
reg[name] = self

>>> Person("olle")
>>> reg["olle"].name

"olle"

I think there are thousand different ways you could solve it.

--
mvh Björn
 
Reply With Quote
 
 
 
 
Adam Atlas
Guest
Posts: n/a
 
      12-24-2006
On Dec 23, 5:58 pm, "BJörn Lindqvist" <(E-Mail Removed)> wrote:
> On 23 Dec 2006 14:38:19 -0800, Adam Atlas <(E-Mail Removed)> wrote:
>
> > Is it possible for an object, in its __init__ method, to find out if it
> > is being assigned to a variable, and if so, what that variable's name
> > is? I can think of some potentially ugly ways of finding out using
> > sys._getframe, but if possible I'd prefer something less exotic.
> > (Basically I have a class whose instances, upon being created, need a
> > 'name' property, and if it's being assigned to a variable immediately,
> > that variable's name would be the best value of 'name'; to make the
> > code cleaner and less redundant, it would be best if it knew its own
> > name upon creation, just like functions and classes do, without the
> > code having to pass it its own name as a string.)I guess you mean something like this:

>
> >>> olle = Person()
> >>> olle.name"olle"

>
> Instead of:
>
> >>> olle = Person("olle")
> >>> olle.name"olle"

>
> It is not possible without ugly hacks. What you could use instead is
> some kind of registry approach:
>
> reg = {}
> class Person:
> def __init__(self, name):
> self.name = name
> reg[name] = self
>
> >>> Person("olle")
> >>> reg["olle"].name"olle"

>
> I think there are thousand different ways you could solve it.


Yeah, I've thought of ways like that. I was just hoping to make the
syntax as minimal and Pythonic as possible.

I have the following working:

> import sys
>
> class c:
> def __init__(self):
> f = sys._getframe(1)
> names = [n for n in f.f_code.co_names if n not in f.f_locals]
> if len(names) > 0:
> name = names[0]
> print name
>
> a = c() # prints 'a'
> b = 'blah'
> b = c() # prints nothing


Question: too evil?

 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      12-24-2006
On Sat, 23 Dec 2006 14:38:19 -0800, Adam Atlas wrote:

> Is it possible for an object, in its __init__ method, to find out if it
> is being assigned to a variable, and if so, what that variable's name
> is?


What should the variable name be set to if you do one of the following?


john = eric = graham = terry = Named_Instance()

some_list = [None, 1, "string", Named_Instance()]

fred = Named_Instance(); barney = fred; del fred


Name assignment is not a one-to-one operation. An object can have no name,
one name or many names. If your code assumes such a one-to-one
relationship between names and objects, it is wrong.


> I can think of some potentially ugly ways of finding out using
> sys._getframe, but if possible I'd prefer something less exotic.
> (Basically I have a class whose instances, upon being created, need a
> 'name' property, and if it's being assigned to a variable immediately,
> that variable's name would be the best value of 'name'; to make the code
> cleaner and less redundant, it would be best if it knew its own name
> upon creation, just like functions and classes do, without the code
> having to pass it its own name as a string.)


I suggest rethinking your data model, and accept that the name
attribute of an object is not necessarily the same as the name it is
bound to.

If you still want a convenience function that names the object and binds
it to a name at the same time, try something like this:

def Make_A_Named_Instance(name, *args, **kwargs):
globals()[name] = Named_Instance(*args, **kwargs)
globals()[name].name = name


You might be tempted to replace globals() with locals() in the above.
Don't -- it doesn't generally work:

http://docs.python.org/lib/built-in-funcs.html


--
Steven.

 
Reply With Quote
 
Steven Bethard
Guest
Posts: n/a
 
      12-24-2006
Adam Atlas wrote:
> Is it possible for an object, in its __init__ method, to find out if it
> is being assigned to a variable, and if so, what that variable's name
> is? I can think of some potentially ugly ways of finding out using
> sys._getframe, but if possible I'd prefer something less exotic.
> (Basically I have a class whose instances, upon being created, need a
> 'name' property, and if it's being assigned to a variable immediately,
> that variable's name would be the best value of 'name'; to make the
> code cleaner and less redundant, it would be best if it knew its own
> name upon creation, just like functions and classes do, without the
> code having to pass it its own name as a string.)


As others have mentioned, in general the answer is no. However, class
statements do have access to the name they're assigned, so you could
abuse a class statement like this::

>>> # your class whose instances need a name property
>>> class C(object):

... def __init__(self, name):
... self.name = name
... @classmethod
... def from_class_block(cls, name, bases, blockdict):
... return cls(name)
...
>>> # instances of your class with the appropriate names
>>> class instance:

... __metaclass__ = C.from_class_block
...
>>> instance.name

'instance'

Though it doesn't rely on private functions like sys._getframe, it's
still sure to confuse the hell out of your users.

STeVe
 
Reply With Quote
 
Adam Atlas
Guest
Posts: n/a
 
      12-24-2006
Thanks, Steven and Steven.

@Bethard:
Isn't it a bit convoluted to use metaclasses?
someinstance.__class__.__name__ does the same thing.

@D'Aprano:
Thanks for the advice to rethink my data model. I'm doing so right now,
and I've already come up with a way that makes more sense.

 
Reply With Quote
 
Steven Bethard
Guest
Posts: n/a
 
      12-24-2006
Adam Atlas wrote:
> Isn't it a bit convoluted to use metaclasses?


Yep. It's a well known fact that putting "convoluted" and "metaclasses"
in the same sentence is repetitively redundant.

> someinstance.__class__.__name__ does the same thing.


No, not really::

>>> class C(object):

... def __init__(self, name):
... self.name = name
... @classmethod
... def from_class_block(cls, name, bases, blockdict):
... return cls(name)
...
>>> c = C('foo')
>>> c.name

'foo'
>>> type(c)

<class '__main__.C'>
>>> c.__class__.__name__

'C'
>>> class foo:

... __metaclass__ = C.from_class_block
...
>>> foo.name

'foo'
>>> type(foo)

<class '__main__.C'>
>>> foo.__class__.__name__

'C'

Note that the ``class foo`` statement is not creating a class. It's
creating an instance of ``C``. So it really is doing something pretty
different.

STeVe
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
adding a variable name to a hash to name is part of the variable name Bobby Chamness Perl 2 04-22-2007 09:54 PM
Assignment operator self-assignment check Chris C++ 34 09-26-2006 04:26 AM
Augument assignment versus regular assignment nagy Python 36 07-20-2006 07:24 PM
print("my name is {name}, and {age}-year old {gender}", name, age, gender); =?iso-8859-1?B?bW9vcJk=?= Java 7 01-02-2006 04:39 PM
Re: Urgent! how to get object name, method name and attribute name based on the strings? ding feng C++ 2 06-25-2003 01:18 PM



Advertisments