Python 是否可以从另一个对象调用对象
我正在尝试为pygame中的颜色创建一个类。 这就是我现在拥有的Python 是否可以从另一个对象调用对象,python,class,Python,Class,我正在尝试为pygame中的颜色创建一个类。 这就是我现在拥有的 class color: white = (255, 255, 255) black = (0, 0, 0) red = (200, 0 ,0) green = (0, 200, 0) blue = (0, 0, 200) brightRed = (255, 0, 0) darkRed = (127, 0, 0) color
class color:
white = (255, 255, 255)
black = (0, 0, 0)
red = (200, 0 ,0)
green = (0, 200, 0)
blue = (0, 0, 200)
brightRed = (255, 0, 0)
darkRed = (127, 0, 0)
color = color()
print (color.brightRed)
这将按预期打印(255,0,0)。
我想知道有没有可能做出这样的事情:
color = color()
print(color.red)
print(color.red.bright)
print(color.red.dark)
所以我可以得到下一张照片:
(200, 0 ,0)
(255, 0, 0)
(127, 0, 0)
我知道这个例子是没有意义的,但如果可能的话,我可以做一些其他的事情。您可以定义另一个类
class Red:
bright = (255, 0, 0)
dark = (127, 0, 0)
normal = (200, 0, 0)
class color:
white = (255, 255, 255)
black = (0, 0, 0)
red = Red
green = (0, 200, 0)
blue = (0, 0, 200)
brightRed = Red.bright
darkRed = Red.dark
color = color()
print(color.red.normal)
print(color.red.bright)
print(color.red.dark)
不能将
color.red
同时作为用户定义的类和元组,因此不能使用color.red
代替color.red.normal
。但是您可以使用normalRed=Red.normal
然后使用color.normalRed
定义另一个具有明亮属性和黑暗属性的类,并将其指定给颜色.Red
。非常感谢您