子类默认参数继承自姐妹子类

时间:2018-06-08 18:06:43

标签: python class inheritance global-variables

是否有更优雅的pythonic方法将子类的类参数设置为其范围之外的默认值?运行我的分析的闭源软件写出了我无法控制的语法。

我正在尝试全局,但我从阅读S.O.这不是正确的方法。

DefaultColor = "yellow"

# class_Car.py
class Car:
    def __init__(self):
        self.name = "car"
        self.DefaultColor = "red"
        self.color = "green"

# class_Color.py
class Color(Car):
    def __init__(self, color):
        self.color = color

    def set_default(self):
        global DefaultColor             # ugly code
        DefaultColor = self.color

# class_wheel.py
class Wheel(Car):
    def __init__(self):
        self.color = DefaultColor   

# non-mutable code due to how external program writes out syntax by default
blue_color = Color("blue")
blue_color.set_default()

wheel = Wheel()

# test
print(DefaultColor)         # blue
print(blue_color.color)     # blue
print(wheel.color)          # blue

0 个答案:

没有答案