samedi 3 juin 2017

Update self variable within __init__ everytime it is called in python

I want a self variable within init to update every time it is called e.g. every time I execute Data(10).plot, self.plot should reinitialise by parsing the self.n to the Plot class.

class Data(object):
    def __init__(self, n):
        self.n = n
        self.plot = Plot(self.n)

    def minus(self, x):
        self.n -= x
        return self.n


class Plot(object):
    def __init__(self, n):
        self.n = n

    def double(self):
        return self.n * 2

Another example: When I execute the following code, I want the answer variable to equal 16. Instead it equals 20. How do I implement this behaviour within the above classes?

data = Data(10)
data.minus(2)
answer = vcf.plot.double())

Aucun commentaire:

Enregistrer un commentaire