Addressing an instance name string in __init __ (self) in Python

I am doing something like this:

class Class(object): def __init__(self): self.var=#new instance name string# 

How do I get the __ init __ method of my instance to use the instance name string for 'c'? Let's say in the case of:

 c=Class() 

I want c.var to be equal to 'c'.

Thanks for your answers, I use persistence, and Class is a constant class of objects. I want __ init __ to add a record to the database when:

 c=Class() 

Then suppose:

 del c 

Later:

 c=Class() 

sholuld create an instance using the data from the database if there is already an 'c' entry, otherwise create a new entry.


Thanks for your answers, I use persistence, and Class is a constant class of objects. I want __ init __ to add a record to the database when:

 c=Class() 

Then suppose:

 del c 

Later:

 c=Class() 

sholuld create an instance using the data from the database if there is already an 'c' entry, otherwise create a new entry.

+4
source share
9 answers

To save data objects, you need to use a unique identifier for the database record.

pesudo because i don't know which database module you are using

 import db # assume this is your db module class Class(object): def __init__(self): self.id = None self.name = None def get_by_id(self, id): records = db.execute('select * from table where id=%s' % str(id)) if records: self.id = records[0]['id'] self.name = records[0]['name'] def save(self): db.execute('update table set name=%s where id=%s' % (self.name, str(self.id))) 

Again, this is pseudo code, the string input method that I use is NOT recommended as being quite insecure, it's just there to illustrate how to continue using classes with db.

+1
source

Python has no variables; it has objects and names . When you do

 c = Class() 

you do two things:

  • Creating a new object of type Class
  • Associating an object named c in the current area.

The created object does not have the concept of a variable name. If you do later

 a = c 

then the same object is accessible in exactly the same way using the names a and c . You can remove the name a , and the object will still exist.

If the objects you create must have a name, the best way is to pass it explicitly ,

 class Class(object): def __init__(self, name): self.name = name var = Class('var') 
+7
source

You cannot do this. The reason for this is that the class object is created first, and only after that this object is bound to the instance name.

+3
source

You cannot (with the exception of incredible hacks such as checking the frame of the stack and checking bytecode). There may not even be a name, or there may be several such names. What you need to specify for the following code snippets, for example:

 l = [Class(), Class()] a=b=c=d=Class() 
+2
source

I do not think that this would be possible, because the assignment of the variable to your new instance occurs after the object is completely constructed and initialized, and therefore you do not know the name of the variable into which it will be assigned the value init

+2
source

I do not know how to access the variable name programmatically without using deep reflection and a debugger. I do not think the information is available at runtime.

If you want to give instances a name (unique?), You should probably make the initializer an extra argument.

 def __init__(self, name): self.name = name 

And the caller must pass the appropriate name:

 c = Class("c") 
+1
source

This is a problem with the area; you cannot do what you ask. Since c will be declared outside the scope of your class, your instance does not know what it was called in the code.

Perhaps if you can give a broader explanation of what you are trying to achieve, you can offer a better solution.

+1
source

It's impossible. It seems you are mixing variables and objects.

In any case, there might not be a variable:

eg.

 foo(Class()) Class().arbitraryMethod() 

Or a few:

 a = b = Class() 
0
source

I have the same idea a few years ago. This is some neat feature, but the creator of the language does not provide it. And I thought that they are all fools, so as not to discover this wonderful feature.

But then think about it. I think logic is impossible. they say:

 class Class(object): def __init__(self): self.instance_name.move() # self.instance_name refer to var def move(self): print "move" var = Class() 

now if var is an array, is that also possible?

 var[0] = Class() # i think it will get confused a bit 

what I think, I don’t think that assigning an instance to myself is possible. and in some language I just sent the instance string to the object and then used eval to execute the function

0
source

All Articles