Getting the name of an assignment

A

Adam Atlas

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.)
 
?

=?ISO-8859-1?Q?BJ=F6rn_Lindqvist?=

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"

Instead of:
"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.
 
A

Adam Atlas

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:

Instead of:

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?
 
S

Steven D'Aprano

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
 
S

Steven Bethard

Adam said:
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::
... def __init__(self, name):
... self.name = name
... @classmethod
... def from_class_block(cls, name, bases, blockdict):
... return cls(name)
... ... __metaclass__ = C.from_class_block
... '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
 
A

Adam Atlas

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. :)
 
S

Steven Bethard

Adam said:
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::
... def __init__(self, name):
... self.name = name
... @classmethod
... def from_class_block(cls, name, bases, blockdict):
... return cls(name)
... ... __metaclass__ = C.from_class_block
... '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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top