how to get a class instance name during creation?

D

dmitrey

hi all,
I have a code
z = MyClass(some_args)
can I somehow get info in MyClass __init__ function that user uses "z"
as name of the variable?

I.e. to have __init__ function that creates field z.name with value
"z".

Thank you in advance, D.
 
B

Bruno Desthuilliers

dmitrey a écrit :
hi all,
I have a code
z = MyClass(some_args)
can I somehow get info in MyClass __init__ function that user uses "z"
as name of the variable?

I.e. to have __init__ function that creates field z.name with value
"z".

This has been debated to hell and back. To make a long story short, here
are two common use cases:

x = MyClass()
y = x
del x

objects = [MyClass() for i in range(100)]

If you can come with a meaningfull answer to "what's *the* name of any
of the MyClass instance(s)" in both cases, then please let us know...
 
G

Gabriel Genellina

I have a code
z = MyClass(some_args)
can I somehow get info in MyClass __init__ function that user uses "z"
as name of the variable?

I.e. to have __init__ function that creates field z.name with value
"z".

No. I'd just write it as:

z = MyClass(some, args, 'z')

wich is pretty simple and readable (although you have to repeat the 'z').
All the alternative ways I know of require too much black magic.
 
A

Aaron \Castironpi\ Brady

dmitrey a écrit :
hi all,
I have a code
z = MyClass(some_args)
can I somehow get info in MyClass __init__ function that user uses "z"
as name of the variable?
I.e. to have __init__ function that creates field z.name with value
"z".

This has been debated to hell and back. To make a long story short, here
are two common use cases:

x = MyClass()
y = x
del x

objects = [MyClass() for i in range(100)]

If you can come with a meaningfull answer to "what's *the* name of any
of the MyClass instance(s)" in both cases, then please let us know...

Hmmm, just thinking:

What about a function:

autoname( 'x= MyClass' )

which calls exec as well as determines the name? That could use
'code' and 'ast' modules and 'parse', or just take the first \w
characters, assuming assignment, or:

autoname( 'x', MyClass() )

which executes assignment and sets an attr or calls a method on the
second arg.
 
D

dmitrey

dmitrey a écrit :
hi all,
I have a code
z = MyClass(some_args)
can I somehow get info in MyClass __init__ function that user uses "z"
as name of the variable?
I.e. to have __init__ function that creates field z.name with value
"z".

This has been debated to hell and back. To make a long story short, here
are two common use cases:

x = MyClass()
y = x
del x

objects = [MyClass() for i in range(100)]

If you can come with a meaningfull answer to "what's *the* name of any
of the MyClass instance(s)" in both cases, then please let us know...

I had seen the examples during google search, still I hoped for an
answer to my exact situation. I know for sure there will be no
renaming and creating like the above objects = [MyClass() for i in
range(100)].
as for the z = MyClass(some, args, 'z') I had it in my mind but I
hoped to have something automatic, w/o the arg 'z'.
Regards, D.
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top