detmining name during an assignment

Stephen Hansen apt.shansen at gmail.com
Fri Sep 18 19:14:37 CEST 2009


On Fri, Sep 18, 2009 at 10:00 AM, Jamie Riotto <jamie.riotto at gmail.com>wrote:

> I have an app that uses Python scripting. When a user creates a new object:
>
> objName = newObject()
>
> I'd like the newObject be able to use objName as its internal name.
>

Almost this exact same question came up not so long ago, which is a bit
strange.

The short of it is, you can't.


> So, if a user says:
>
> cube1 = Cube()
>
>  A "cube1" object should apear in the scene list. I believe this means I
> need
> to determine that a namespace assignment is going on from inside the
> object's init code
> or by using properties.
>

That's the thing: during the object's __init__, a "namespace assignment"
isn't going on. newObject() initializes and creates an object, returning it.
There's no way for it to tell what happens to that object after. It might
just get discarded and garbage collected immediately. Or it might get
inserted into a dictionary. Or it might end up bound to a name in a
namespace. It has no idea: all of that happens completely -after- the
__init__ is completely finished and returned.

There isn't a simple answer because Python just doesn't work like that :) An
assignment statement doesn't create a variable or a cell or anything of that
nature, it simply labels an object with a particular name that's only
meaningful in a certain namespace. A given object can have any number of
such labels and the object can't actually tell what those are.

--S
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090918/39fb4eef/attachment.html>


More information about the Python-list mailing list