473,387 Members | 1,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

how to get a class instance name during creation?

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.
Oct 3 '08 #1
4 948
En Fri, 03 Oct 2008 15:04:01 -0300, dmitrey <dm*************@scipy.org>
escribió:
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.

--
Gabriel Genellina

Oct 3 '08 #2
On Oct 3, 1:46*pm, Bruno Desthuilliers
<bdesth.quelquech...@free.quelquepart.frwrote:
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.
Oct 4 '08 #3
On Oct 3, 9:46*pm, Bruno Desthuilliers
<bdesth.quelquech...@free.quelquepart.frwrote:
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.
Oct 4 '08 #4
dmitrey <dm*************@scipy.orgwrites:
On Oct 3, 9:46Â*pm, Bruno Desthuilliers
<bdesth.quelquech...@free.quelquepart.frwrote:
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)].
You *know* this, *for sure*? The only way I can think of that would
give you such certain knowledge that such a situation will not happen
is an automated, full-coverage unit test suite of all code that uses
your class. Which is an excellent position to be in, so I commend you
on your diligence.

So, why is it that you wish to restrict users of your class to never
do such normal operations with instances as in the above examples?
What problem are you solving by this restriction?

--
\ “Holy human pressure cookers, Batman!†—Robin |
`\ |
_o__) |
Ben Finney
Oct 4 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Sridhar R | last post by:
Consider the code below, class Base(object): pass class Derived(object): def __new__(cls, *args, **kwds): # some_factory returns an instance of Base # and I have to derive from this...
6
by: Andre Meyer | last post by:
Hi all I have been searching everywhere for this, but have not found a solution, yet. What I need is to create an object that is an instance of a class (NOT a class instance!) of which I only...
23
by: Fabian Müller | last post by:
Hi all, my question is as follows: If have a class X and a class Y derived from X. Constructor of X is X(param1, param2) . Constructor of Y is Y(param1, ..., param4) .
1
by: Taylor | last post by:
"Static" is synonymous with "class" with regards to members (methods and fields) right? Sometimes I hear refrence to the "static method" and sometimes its refered to a "static method." Is there...
10
by: Opa | last post by:
I have tried for two days to solve this problem with no luck. I have a singleton class which has some events declared. When I inherit from this class the events don't seem to come along with it....
5
by: Russell Warren | last post by:
I just ran across a case which seems like an odd exception to either what I understand as the "normal" variable lookup scheme in an instance/object heirarchy, or to the rules regarding variable...
37
by: Joergen Bech | last post by:
(Slightly religious question): Suppose I have the following class: ---snip--- Public Class MyClass Private _MyVariable As Integer Public Property MyVariable() As Integer Get
5
by: Joe Van Dyk | last post by:
Say I have the following class: using std::string; class Player { public: Player() : name(""), age(""), other_stuff("") {} private: string name; string age;
10
by: ying.gary.zhang | last post by:
// T.h: class T { public: static T* instance(); private: T() {} ~T() {} static T* smInstance;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.