简体   繁体   中英

Is it possible to update an object initialized in a superclass from a subclass?

So lets say I have a class:

class SuperClass():
    def __init__(self, number):
       self._inputnumber = number
       self._initallist = []

And then I want to build subclasses of this class which have methods that can add items to the initialized list, read items from it, or remove items from it. For example.

class SubClass(SuperClass):
     def __init__(self, number, current_line, new_line): 
        self._number = number
        self._thisline = current_line
        self._gohere = new_line
     def execute(self):
        (SuperClass._initallist).append(self._thisline + 1)

This is a bit of a rough example of what I am trying to do. I want to be able to have my initial list available for a couple of classes so that they can both act upon it as shown above. What ends up happening in my case, though, is that I get an AttributeError saying that my SuperClass doesn't have the named attribute.

AttributeError: type object 'SuperClass' has no attribute '_initiallist'

Any tips on how to do this? Is it even possible or do I have to do something else to achieve this result?

_initallist is an instance attribute, you can not access it by Super._initallist .

I think what you want to do is below. You need to init SuperClass in the SubClass . And I think if this is a right is-one relation, there need to be a number in SubClass .

class SuperClass():
    def __init__(self, number):
       self._inputnumber = number
       self._initallist = []

class SubClass(SuperClass):
     def __init__(self, number, current_line, new_line):
        SuperClass.__init__(self, number) 
        self._thisline = current_line
        self._gohere = new_line
     def execute(self):
        self._initallist.append(self._thisline + 1)

只做self._initiallist.append(self._thisline + 1)

"I want to build subclasses of this class which have methods that can add items to the initialized list,..."

Are you saying you want the subclasses to inherit the instance attribute _initiallist and provide methods to act upon it? If this is the case then instances of the subclasses will each have an _initiallist attribute but each _initiallist for each instance of the class will be a different list.

In that case the solution is as zhangyangyu described.


"I want to be able to have my initial list available for a couple of classes so that they can both act upon it as shown above."

Based on the title, I could interpret this as: I want the _initiallist attribute of some instance of the superclass to be available for instances of a couple of subclasses so they can all act upon the same exact list.

You could do that if _initiallist is a class attribute, or you could pass an instance of the superclass as an __init__ argument when instantiating the subclass, as far as I know currently.

Also, this would mean that the two classes in your example do not posses an is a relationship, but rather, your superclass is really a sharedobject (AFAIK).

If you make it a class attribute then you lose some flexibility because then different instances of the superclass will all have an attribute _initiallist that points to the same exact list. Where as if you keep it as an instance attribute and pass an instance of the superclass to the subclass as an argument (as a shared object), then you can still achieve what you want to do and still have the flexibility to create multiple instances of your shared object.

class SharedObject(object):

    def __init__(self, number):
        self._inputnumber = number
        self._initiallist = []


class Client(object):

    def __init__(self, obj, current_line, new_line):
        self._sharedobj = obj
        self._thisline = current_line
        self._gohere = new_line

    def execute(self):
        self._sharedobj._initiallist.append(self._thisline + 1)

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