Python: How to use First Class Object Constructor value In another Object
python
Solution
Assign it as property to the class:
>>> class MyClass(object):
def __init__(self, x=None):
if x is not None:
self.__class__.x = x
def do_something(self):
print self.x # or self.__class__.x, to avoid getting instance property
>>> my_class1 = MyClass('aaa')
>>> my_class2 = MyClass()
>>> my_class2.do_something()
aaa
Problem
``` class MyClass(Object): def __init__(self, x=None): if x: self.x = x def do_something(self): print self.x ``` Now I have two objects `my_class1 = MyClass(x)` `my_class2 = MyClass()` I want to use x when this my_class2 object is called As other languages Support static variable like java,c++ etc.