简体   繁体   English

如何在python中定义一个抽象类并强制实现变量

[英]how to define an abstract class in python and force implement variables

So, I am trying to define an abstract base class with couple of variables which I want to to make it mandatory to have for any class which "inherits" this base class.. So, something like: 所以,我试图定义一个带有几个变量的抽象基类,我想让它对任何“继承”这个基类的类都必须要有。所以,类似于:

class AbstractBaseClass(object):
   foo = NotImplemented
   bar = NotImplemented

Now, 现在,

class ConcreteClass(AbstractBaseClass):
    # here I want the developer to force create the class variables foo and bar:
    def __init__(self...):
        self.foo = 'foo'
        self.bar = 'bar'

This should throw error: 这应该抛出错误:

class ConcreteClass(AbstractBaseClass):
    # here I want the developer to force create the class variables foo and bar:
    def __init__(self...):
        self.foo = 'foo'
        #error because bar is missing??

I maybe using the wrong terminology.. but basically, I want every developer who is "implementing" the above class to force to define these variables?? 我可能使用了错误的术语..但基本上,我希望每个“实现”上述类的开发人员强制定义这些变量?

Update : abc.abstractproperty has been deprecated in Python 3.3. 更新abc.abstractproperty已在Python 3.3中弃用。 Use property with abc.abstractmethod instead as shown here . 使用propertyabc.abstractmethod如图所示,而不是在这里

import abc

class AbstractBaseClass(object):

    __metaclass__ = abc.ABCMeta

    @abc.abstractproperty
    def foo(self):
        pass

    @abc.abstractproperty
    def bar(self):
        pass

class ConcreteClass(AbstractBaseClass):

    def __init__(self, foo, bar):
        self._foo = foo
        self._bar = bar

    @property
    def foo(self):
        return self._foo

    @foo.setter
    def foo(self, value):
        self._foo = value

    @property
    def bar(self):
        return self._bar

    @bar.setter
    def bar(self, value):
        self._bar = value
class AbstractBaseClass(object):
    def __init__(self):
        assert hasattr(self, 'foo')
        assert hasattr(self, 'bar')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Python 中为抽象类定义构造函数实现? - How define constructor implementation for an Abstract Class in Python? 如何在python的继承类中将抽象类方法实现为静态方法? - How to implement abstract class methods as static method in inherited class in python? Python中是否有“Addable”协议或抽象基class? 如果不是,人们将如何定义它? - Is there an "Addable" protocol or abstract base class in Python? If not how would one define it? 如何在python中定义抽象元类 - How to define an abstract metaclass in python 如何在OpenERP上实现抽象类? - How to implement abstract class on OpenERP? 抽象类可以强制继承类将方法实现为静态方法吗? - Can an abstract class force the inheriting class to implement a method as static? Python3 - 如何从现有抽象类定义抽象子类? - Python3 - How does one define an abstract subclass from an existing abstract class? 如何在 Python 中使用实例的函数和变量定义类变量 - How to define class variables using the functions and variables of an instance in Python 如何强制抽象类的子类应该在 Python 中定义一个特定的内部类 - How to enforce that subclass of abstract class should define a particular inner class in Python 如何为Python中的抽象class创建抽象属性? - How to create an abstract attribute for an abstract class in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM