3

I have a python class defined in a module1.py file:

class MBVar(): def __init__(self, var_type, expression): self.var_type = var_type self.expression = expression ... ecc ... 

I would like to be able to write in a main *.py file:

from module1 import MBVar X = MBVar('integer', 6) 

and add to my MBVar class:

self.name = ??? 

in such a way that: self.name = 'X'. Is it possible to do this??

Thanks

9
  • 3
    Not really. A python object doesn't generally know what name you've bound it to unless you tell it. Consider what would happen if you did X = MBVar('integer', 6); Y = X; Z = Y? What would you expect the .name attribute of X, Y and Z to contain? Commented May 2, 2016 at 14:02
  • 1
    Why do you want to do that? I don't think that what you want is possible but i think that there is an other way to do what you want to do, at the end Commented May 2, 2016 at 14:30
  • 1
    @mattia: I get that. But MBVar has no way of knowing the name that you bind the instance to, unless you explicitly tell it. And as I mentioned earlier, what should the .name become if you bind the instance to multiple names? Commented May 2, 2016 at 14:32
  • 1
    Here's another scenario: print([MBVar('integer', i) for i in range(5)]). That creates 5 MBVar instances, and none of them are actually bound to a name. So what would you want your magical naming operation to do in that situation? Commented May 2, 2016 at 14:35
  • 1
    You may find this article helpful: Facts and myths about Python names and values, which was written by SO veteran Ned Batchelder. Commented May 2, 2016 at 14:37

2 Answers 2

1

So I Assume you want to pass variable name and value as parameter and assign it to an object, to do that we don't need the type of the variable since python uses duck typing we just have to add the string representation of the variable name in the inbuilt dictionary __dict__ as key and the integer as value.

class MBVar(): def __init__(self, var_name, expression): self.__dict__[var_name] = expression def add_later(self, var_name, expression): self.__dict__[var_name] = expression def get_name(self): return self.name X = MBVar('name', 6) print X.get_name() # prints 6 X.add_later('secint',4); print X.secint #prints 4 X.__dict__['thirdint'] = 7 print X.thirdint #prints 7 
Sign up to request clarification or add additional context in comments.

2 Comments

This answer doesn't not appear to address the OP's question.
@PM2Ring It seems the OP isn't clear about the question so that I carefully started with an assumption. will be glad to remove it if my assumption is wrong. I would have added a comment instead but adding this entire thing in comment will be an overkill i thought
0

I have a solution but i don't think that this is a very good coding practice. Moreover, it is a 2 steps process: it can't be done inside the __init__ method because till the end of this method, the object has not been yet associated to a variable.

class Foo: def __init__(self): self.__name = "" def set_name(self, name): self.__name = name def get_name(self): return self.__name if __name__ == "__main__": a = Foo() b = Foo() c = Foo() dict_v = locals() v = "" # this line initialize the variable of name "v" because of its entry in the locals() dict #-> prevent "RuntimeError: dictionary changed size during iteration " for v in dict_v.keys(): if isinstance(dict_v[v], Foo): # the process only happens for the objects of a specific class dict_v[v].set_name(v) #proof print(a.get_name()) print(b.get_name()) print(c.get_name()) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.