简体   繁体   中英

change class instance attribute conditionally in python

I'm a newbie in python. I have a class X inheriting class Y. In class X the attribute b allways keep constant and never change but in the class Y it must change when a given condition is satisfied.

class X(object):

   def __init__(self,others_attributes):
    
       self.b = 1
       self.others_attributes = others_attributes

class Y(X):

   def __init__(self,others_attributes,variable_condition):
        
        super(X, self).__init__(others_attributes)

    self.b += 1

How can ensure that each simulation step any instance of the class Y will have a specific value of the attribute b? Shall I create a @classmethod or a @properties similar to something like below? Or is there a better strategy?

if variable_condition:
    self.b = self.b + 1
else:
    self.b = self.b

return self.b

Not sure if you're asking about maintaining class level state or instance level state.

If you're asking about how to change class level state all you need to do is store b as a class variable:

some_condition = True

class X(object):
    count = 0

class Y(X):
    def __init__(self):
        if some_condition:
            Y.count += 1

instances = [
    X(),
    Y(),
    Y(),
    X(),
    X(),
    Y()
]

print('\n'.join([str(i.count) for i in instances]))

If you want to change the b for only certain instances of Y then you could do something like:

class X(object):
    def __init__(self):
        self.b = 0

class Y(X):
    def __init__(self, some_condition):
        super(Y, self).__init__()
        if some_condition:
            self.b += 1  # or set it to whatever else you want
        else:
            self.b = -1

instances = [
    X(),
    Y(True),
    Y(False),
    X(),
    X(),
    Y(True)
]

print('\n'.join([str(i.b) for i in instances]))

@kingkupps

considering as bellow

some_condition = True

class X(object):
    def __init__(self,a,c):
        self.b = 1
        self.a = a
        self.c = c

class Y(X):

   def __init__(self,a,c):

        super(X,self).__init__(a,c)

        def __init__(self, some_condition):
            super(Y, self).__init__()
            if some_condition:
               self.b += 1  # or set it to whatever else you want
            else:
               self.b = -1

File "C:\\Users\\Alexandre\\Documents\\PYTHON\\Paper_ICESP\\teste.py", line 248, in ABM cells.myfun(a,c)

File "C:\\Users\\myname\\firstfolder\\secondfolder\\thirdfolder\\myscript.py", line 132, in myfun c = Y(a,c)

File "C:\\Users\\myname\\firstfolder\\secondfolder\\thirdfolder\\myscript.py", line 154, in init super(X,self). init (a,c)

TypeError: object. init () takes exactly one argument (the instance to initialize)

I suspect that creating a superclass Y inside daughter class Y is interfering in Y. What do you think?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM