I think this is actually impossible, since the assignment operation only
happens AFTER the __init__ is done:



class foo():
        def __init__(self):
                raise Exception("Error!")

try:
        x = foo() 
except:
        pass

print x

# Error: name 'x' is not defined



And even if you were able to get it to work, what would you expect would
happen here?


>>> f = foo()
I'm assigned to 'f'!
>>> x = f
I'm also assigned to 'x'?...


or here?

>>> d = {'a' : [1,2,3, foo()]}
"I'm assigned to the 4th element in a list within dictionary 'd'?..."




-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of AKEric
Sent: Friday, May 01, 2009 10:49 AM
To: python_inside_maya
Subject: [Maya-Python] Re: object query assigned variable name?


Mainly I'm just trying to get a better grasp on the limits of the
language.  There is so much 'built in' to Python, I sometimes will
find a gem that I was previously unaware of.

To me, it seems obvious that an object should know the string var of
the pointer variable (since the variable 'name' is very well aware of
what memory location it's referencing), but recently been told on
other forums this is basically impossible.  Which is just as valuable
to know imo :)

thanks

On May 1, 10:26 am, John Riddle <[email protected]> wrote:
> Maybe I'm not understanding the need for this... or how you are intending
to
> use it, but if it's going to be hardcoded is it possible to just pass
> itself?? Maybe I'm oversimplifying this so don't laugh at me! haha
>
> class foo():
>     def __init__(self, instancedVarName):
>         self.var = instancedVarName
>         print "I'm assigned to %s" % self.var
>
> >>> f = foo('f')
> >>> I'm assigned to f
>
> Dumb answer?
>
> On Fri, May 1, 2009 at 9:20 AM, Eric Pavey <[email protected]> wrote:
>
> > 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 :)  This
> > isn't realized specifically to Maya-Python, but since I'm trying to
> > implement this in a module I'll use in Maya, I think it's applicable
> > for this list too :)
>
> > Given an object:
>
> > class Foo(object):
> >    def __init__(self):
> >        # stuff....
>
> > I'd like it to print, when instanced, something like this:
>
> > >>> f = Foo()
> > I’m assigned to f!
>
> > 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?
>
>


--~--~---------~--~----~------------~-------~--~----~
http://groups.google.com/group/python_inside_maya
-~----------~----~----~----~------~----~------~--~---

Reply via email to