Я создал объект класса с именем «Node». Затем я создал два подкласса «Beetle» и «Dakota». Вы заметите, что есть attr, называемый «суперкласс», который является частью базового слоя. Я хочу, чтобы этот атрибут был установлен для каждого подкласса, и как только он установлен, он никогда не должен изменяться. Это атрибут только для чтения. Мне было интересно, как правильно установить этот атрибут, чтобы быть атрибутом только для чтения?python: установить атрибут только для чтения для объектов класса
class Node(object):
def __init__(self, name, superclass, attributes, children):
self.name = name
self.superclass = superclass
self.attributes = attributes if attributes is not None else {}
self.children = children if children is not None else []
class Beetle(Node):
def __init__(self, name="", superclass="Cars", attributes=None, children=None, enabled=True):
super(Beetle, self).__init__(name=name, superclass=superclass, attributes=attributes, children=children)
self.enabled = enabled
class Dakota(Node):
def __init__(self, name="", superclass="Trucks", attributes=None, children=None, enabled=True):
super(Dakota, self).__init__(name=name, superclass=superclass, attributes=attributes, children=children)
self.enabled = enabled
Ответы на [этот вопрос] (http://stackoverflow.com/questions/14594120/python-read-only-property) кажутся актуальными. – kwinkunks