object query assigned variable name?

W

warpcat

I've passed this around some other groups, and I'm being told
"probably not possible". But I thought I'd try here as well :) I
*did* search first, and found several similar threads, but they
quickly tangented into other specifics of the language that were a bit
over my head :) At any rate, here's a simple example, I'd love to
know if as shown, is somehow possible:

Given an object:

class Spam(object):
def __init__(self):
# stuff....

I'd like it to print, when instanced, something like this:
I’m assigned to s!

But it seems prohibitively hard (based on my web and forum searches)
for an object to know what variable name is has been assigned to when
created. Querying 'self' in __init__ returns a memory location, not
the variable name passed in.

If you're wondering why I'm trying to figure this out, this is just
part of my continued learning of the language and pushing the bounds,
to see what is possible ;)

Any thoughts?
 
S

Steven D'Aprano

I'd like it to print, when instanced, something like this:

I’m assigned to s!

But it seems prohibitively hard (based on my web and forum searches) for
an object to know what variable name is has been assigned to when
created.

Can't be done. Objects don't know what names they are bound to.

Furthermore, your question is based on an invalid assumption. Objects
aren't necessarily bound to a single name, they can be bound to zero,
one, or more names.

What name should Spam print in these examples?

alist = [1, 2, Spam(), 4]
alist.append(Spam())
x = y = z = Spam()
 
D

David Robinow

I've passed this around some other groups, and I'm being told
"probably not possible".  But I thought I'd try here as well :)   I
*did* search first, and found several similar threads, but they
quickly tangented into other specifics of the language that were a bit
over my head :)  At any rate, here's a simple example, I'd love to
know if as shown, is somehow possible:

Given an object:

class Spam(object):
   def __init__(self):
       # stuff....

I'd like it to print, when instanced, something like this:

I’m assigned to s!

But it seems prohibitively hard (based on my web and forum searches)
for an object to know what variable name is has been assigned to when
created.  Querying 'self' in __init__ returns a memory location, not
the variable name passed in.

If you're wondering why I'm trying to figure this out, this is just
part of my continued learning of the language and pushing the bounds,
to see what is possible ;)

Any thoughts?
Others have explained to you that this is not possible.
I'll just point out that your method for learning the language is not optimal.
If you had gotten a recipe to do what you asked, how would it help you
write better programs?
I suggest that you start programming. If you get stumped, feel free to ask here.
 
A

AKEric

I'd like it to print, when instanced, something like this:
I’m assigned to s!
But it seems prohibitively hard (based on my web and forum searches) for
an object to know what variable name is has been assigned to when
created.

Can't be done. Objects don't know what names they are bound to.

Furthermore, your question is based on an invalid assumption. Objects
aren't necessarily bound to a single name, they can be bound to zero,
one, or more names.

What name should Spam print in these examples?

alist = [1, 2, Spam(), 4]
alist.append(Spam())
x = y = z = Spam()

And getting back all the pointers is fine with me. But this sounds
like a dead end, and I was just looking for confirmation of that.
Thanks
 
A

AKEric

Others have explained to you that this is not possible.
I'll just point out that your method for learning the language is not optimal.
If you had gotten a recipe to do what you asked, how would it help you
write better programs?
I suggest that you start programming. If you get stumped, feel free to ask here.

wow

No one has yet told me it isn't possible (until other posts in this
thread), they just alluded to it. Common replies are "well, maybe
this way, or maybe that way". I myself help on other language forums
(that I know far better than Python), and sometimes it's helpful for
someone to just say "no, you can't do it" (if you truly know you
can't), which is what I was looking for here.

But to argue the way I'm learning the language isn't optimal is a bit
short-sited, based on a single-post query. It's valuable to both know
what you can, can't, should and shouldn't do, and one shouldn't be
debased for asking such questions.
 
S

Steven D'Aprano

Others have explained to you that this is not possible. I'll just point
out that your method for learning the language is not optimal. If you
had gotten a recipe to do what you asked, how would it help you write
better programs?

Exactly the same way that knowing that lists have an append() method, but
dicts do not, will help you to write better programs. If you don't know
what tools the language offers, how are you supposed to write *any*
programs, let alone "better" ones?

I suggest that you start programming. If you get stumped, feel free to
ask here.

There's nothing wrong with experimenting with your tools, getting a feel
for what they can do, pushing the boundaries with them. You might not get
any useful code out of such games, but when you start writing code, you
will already know what tools are available.

Warpcat, don't be ashamed about experimenting with Python and learning
the language before getting into "serious" programming. There's
absolutely nothing wrong with such curiosity.
 
J

John O'Hagan

I've passed this around some other groups, and I'm being told
"probably not possible". But I thought I'd try here as well :) I
*did* search first, and found several similar threads, but they
quickly tangented into other specifics of the language that were a bit
over my head :) At any rate, here's a simple example, I'd love to
know if as shown, is somehow possible:

Given an object:

class Spam(object):
def __init__(self):
# stuff....

I'd like it to print, when instanced, something like this:

I’m assigned to s!

If you just want the names an instance has in a given namespace, you could
give your class a method like:

class KnowNames(object):
def get_names(self, namespace):
id_str = str(hex(id(self))[:-1])
return [i for i in namespace if id_str in str(namespace)]

which will give you a list of names when called on an instance.

But if you try moving that method inside __init__(), it returns an empty list
because any assignment is not yet in the namespace.

I can see that it's tantalizing, though, because _somebody_ must know about
the assignment; after all, we just executed it!

Regards,

John
 
D

David Stanek

Can't be done. Objects don't know what names they are bound to.

While objects don't know what they are assigned to, they can be made
to find out. Unless you have a good use case I don't think that you
really want to be doing it.

DecoratorTools allows you to do this. I have code that allows you to
use a a function like:

class C:
attr = inject(Customer)

The inject function does know that it is being assigned to attr. There
is tracing/frame introspection black magic involved. So much so that I
have been debating removing that feature.

The code is on Bitbucket[1]. It has a little extra complication
because inject can also be used as a decorator. The key is the
decorate_assignment call in the inject function.

Again I don't think you really want to do this.

[1] http://bitbucket.org/dstanek/snake-guice/src/tip/snakeguice/decorators.py
 
J

John O'Hagan

]
Given an object:

class Spam(object):
def __init__(self):
# stuff....

I'd like it to print, when instanced, something like this:

I’m assigned to s!

If you just want the names an instance has in a given namespace, you could
give your class a method like:

class KnowNames(object):
def get_names(self, namespace):
id_str = str(hex(id(self))[:-1])
return [i for i in namespace if id_str in str(namespace)]

which will give you a list of names when called on an instance.


And which is a silly way of saying:

class KnowName(object):
def get_names(self, namespace):
return [i for i in namespace if namespace is self]

removing four function calls, an assignment and a slicing operation from a
mere two lines; certainly a personal best for insanely over-wrought code!

Oops. :) ,

John
 
S

Sion Arrowsmith

John O'Hagan said:
I can see that it's tantalizing, though, because _somebody_ must know about
the assignment; after all, we just executed it!

Except we haven't, if we're talking about reporting from the
object's __init__:
.... def __init__(self):
.... print "I'm Brian!"
....
I'm Brian!
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list assignment index out of range

(Yeah, I know that's a setitem call not an assignment. Point stands.
It also demonstrates why the whole idea of "what name is a newly-
created object assigned to" is broken.)
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top