What you want is a property. This is a special type of attribute that calls a custom getter function when getting the value, so you can make it dynamically return the correct plot.
class Data(object):
def __init__(self, n):
self.n = n
@property
def plot(self):
return Plot(self.n)
def __sub__(self, x):
return Data(self.n - x)
As a side note, look at the data model to override python operators.
data = Data(10)
data -= 2
answer = data.plot.double() # Calls the `plot()` function to get a value for `data.plot`.
print(answer) # 16
Another way would be to link the Plot to the data, so when the data changes, the plot does to. A way to do it would be just to have it as an attribute, so when it changes, the attribute does too.
class Plot(object):
def __init__(self, data):
self.data = data
@property
def n(self):
return self.data.n
@n.setter
def n(self, x):
self.data.n = x
def double(self):
return self.n * 2
data = Data(10)
plot = Plot(data)
data.minus(2)
answer = plot.double() # 16
Answer from Artyer on Stack OverflowWhat you want is a property. This is a special type of attribute that calls a custom getter function when getting the value, so you can make it dynamically return the correct plot.
class Data(object):
def __init__(self, n):
self.n = n
@property
def plot(self):
return Plot(self.n)
def __sub__(self, x):
return Data(self.n - x)
As a side note, look at the data model to override python operators.
data = Data(10)
data -= 2
answer = data.plot.double() # Calls the `plot()` function to get a value for `data.plot`.
print(answer) # 16
Another way would be to link the Plot to the data, so when the data changes, the plot does to. A way to do it would be just to have it as an attribute, so when it changes, the attribute does too.
class Plot(object):
def __init__(self, data):
self.data = data
@property
def n(self):
return self.data.n
@n.setter
def n(self, x):
self.data.n = x
def double(self):
return self.n * 2
data = Data(10)
plot = Plot(data)
data.minus(2)
answer = plot.double() # 16
You don't need n in the Data-object:
class Data(object):
def __init__(self, n):
self.plot = Plot(n)
def minus(self, x):
self.plot.n -= x
class Plot(object):
def __init__(self, n):
self.n = n
def double(self):
return self.n * 2
I have this example code for a class:
tableclass.py
class ExampleClass(object):
def __init__(self):
self.EXAMPLE1 = self.set_example_1(False)
self.EXAMPLE2 = self.set_example_2()
def set_example_1(self, element):
if element:
return True
else:
False
def set_example_2(self):
if self.EXAMPLE1 == True:
return "YES"
else:
return "NO" Then I wrote a script to test the results of set_example_2:
# content of test_tableclass.py
from tableclass import ExampleClass
class TestExampleClass:
def test_set_example_2(self):
self.EXAMPLE1 = True
assert ExampleClass.set_example_2(self) == 'YES'
self.EXAMPLE1 = False
assert ExampleClass.set_example_2(self) == 'NO' After running: pytest test_tableclass.py I get the following output:
platform win32 -- Python 3.7.9, pytest-5.4.3, py-1.9.0, pluggy-0.13.1 rootdir: C:\xxx\xxx\xxx\xxx\xxx plugins: mock-3.8.2 collected 1 item test_tableclass.py . [100%]
I always thought that, in order to assign a certain self value you need to make a certain mock-class and then assign the value to that mock-class in example:
mock_class = mock.Mock() mock_class.EXAMPLE1 = True assert ExampleClass.set_example_2(self) == 'YES'
Can somebody explain why the other method of assigning True to self.EXAMPLE1 also works in this case? Or where this is documented?
How do you code a self-updating class variable in python - Stack Overflow
python - update class variable using function defined within self - Stack Overflow
How can I update a "self" variable in Python? - Stack Overflow
python - Is there a way to update 'self' in a class when running the code? - Stack Overflow
if updating one property due to an update on another property is what you're looking for (instead of recomputing the value of the downstream property on access) use property setters:
class SomeClass(object):
def __init__(self, n):
self.list = range(0, n)
@property
def list(self):
return self._list
@list.setter
def list(self, val):
self._list = val
self._listsquare = [x**2 for x in self._list ]
@property
def listsquare(self):
return self._listsquare
@listsquare.setter
def listsquare(self, val):
self.list = [int(pow(x, 0.5)) for x in val]
>>> c = SomeClass(5)
>>> c.listsquare
[0, 1, 4, 9, 16]
>>> c.list
[0, 1, 2, 3, 4]
>>> c.list = range(0,6)
>>> c.list
[0, 1, 2, 3, 4, 5]
>>> c.listsquare
[0, 1, 4, 9, 16, 25]
>>> c.listsquare = [x**2 for x in range(0,10)]
>>> c.list
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Absolutely. But use a property instead.
class SomeClass(object):
def __init__(self, n=5):
self.mylist = range(n)
@property
def listsquare(self):
return [ x**2 for x in self.mylist ]
a = SomeClass()
a.mylist = [4, 5, 8]
print a.listsquare
Caching of the property value is left as an exercise for the reader.
You are confusing classes and instances.
class MyClass(object):
pass
a = MyClass()
MyClass is a class, a is an instance of that class. Your error here is that update is an instance method. To call it from __init__, use either:
self.update(value)
or
MyClass.update(self, value)
Alternatively, make update a class method:
@classmethod
def update(cls, value):
cls.var1 += value
You need to use the @classmethod decorator:
$ cat t.py
class MyClass:
var1 = 1
@classmethod
def update(cls, value):
cls.var1 += value
def __init__(self,value):
self.value = value
self.update(value)
a = MyClass(1)
print MyClass.var1
$ python t.py
2
Class School:
def method_1(self,self2):
self=self2I guess I lose the self for that class. I guess I'm screwed and the method is then pointless as self would always be pointing to the class that called it?
I think what's more common is to either assign specific values to self (e.g. self.new_value=self2.value) or assign the whole self2 to self.self2 (e.g. self.self2 = self2)