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

Getting the name of an assignment

P: n/a
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.)

Dec 23 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On 23 Dec 2006 14:38:19 -0800, Adam Atlas <ad**@atlas.stwrote:
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
Dec 23 '06 #2

P: n/a
On Dec 23, 5:58 pm, "BJörn Lindqvist" <bjou...@gmail.comwrote:
On 23 Dec 2006 14:38:19 -0800, Adam Atlas <a...@atlas.stwrote:
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?

Dec 24 '06 #3

P: n/a
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.

Dec 24 '06 #4

P: n/a
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
Dec 24 '06 #5

P: n/a
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. :)

Dec 24 '06 #6

P: n/a
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
Dec 24 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.