[Tutor] class awareness of variable name
Eric Abrahamsen
eric at abrahamsen.com
Mon Sep 17 14:57:34 CEST 2007
On Sep 17, 2007, at 7:21 PM, Alan Gauld wrote:
>
> "Eric Abrahamsen" <eric at abrahamsen.com> wrote
>
>> When instantiating a class, is it possible for that instance to
>> 'know' (via the __init__ method, I suppose) the name of the variable
>> it's been assigned to?
>
>
> Presumably you have a reason for asking this?
> If you tell us what you are trying to do we might be able to
> come up with a more conventional solution.
To be honest, I was mostly curious, as I couldn't think of a good way
to do it. I was thinking of cleaner ways of giving an instance a
'name' attribute than
instance_name = Class('instance_name')
in which the instance_name is repeated, and could be mistyped. I know
there are problems with multiple bindings, which is why I was
thinking of doing it with the __init__ method, in which case you'd
get the first variable name and then none other (even if it was later
unbound from the original variable). In the end, though, I was mostly
curious by which mechanism you could get the variable name 'inside'
the class workings. How do you pass it in as a string?
Thanks!,
Eric
More information about the Tutor
mailing list