我可以从模块导入一个类,就像导入模块一样:
from module_name import *
这样我每次想要变量时都不必指定“module.class”名称?
例:
在模块中:
class SomeClass:
var1 = "Hello"
var2 = "Bye"
在主脚本中:
def print_something(text):
print(text)
if __name__ == "__main__":
print_something(var1) # "module_name.SomeClass.var1" should not be necessary, only "var1"
修改 在实际的类中,它在实际模块中看起来像什么:
class RGBcolors:
# black-white-color
bwc0 = (0, 0, 0)
bwc1 = (1, 1, 1)
bwc2 = (2, 2, 2)
bwc3 = (3, 3, 3)
bwc4 = (4, 4, 4)
bwc5 = (5, 5, 5)
bwc6 = (6, 6, 6)
bwc7 = (7, 7, 7)
bwc8 = (8, 8, 8)
bwc9 = (9, 9, 9)
bwc10 = (10, 10, 10)
bwc11 = (11, 11, 11)
bwc12 = (12, 12, 12)
bwc13 = (13, 13, 13)
bwc14 = (14, 14, 14)
bwc15 = (15, 15, 15)
bwc16 = (16, 16, 16)
bwc17 = (17, 17, 17)
bwc18 = (18, 18, 18)
bwc19 = (19, 19, 19)
bwc20 = (20, 20, 20)
bwc21 = (21, 21, 21)
bwc22 = (22, 22, 22)
bwc23 = (23, 23, 23)
bwc24 = (24, 24, 24)
bwc25 = (25, 25, 25)
bwc26 = (26, 26, 26)
bwc27 = (27, 27, 27)
bwc28 = (28, 28, 28)
bwc29 = (29, 29, 29)
bwc30 = (30, 30, 30)
bwc31 = (31, 31, 31)
bwc32 = (32, 32, 32)
bwc33 = (33, 33, 33)
bwc34 = (34, 34, 34)
bwc35 = (35, 35, 35)
bwc36 = (36, 36, 36)
bwc37 = (37, 37, 37)
bwc38 = (38, 38, 38)
bwc39 = (39, 39, 39)
bwc40 = (40, 40, 40)
bwc41 = (41, 41, 41)
bwc42 = (42, 42, 42)
bwc43 = (43, 43, 43)
bwc44 = (44, 44, 44)
bwc45 = (45, 45, 45)
bwc46 = (46, 46, 46)
bwc47 = (47, 47, 47)
bwc48 = (48, 48, 48)
bwc49 = (49, 49, 49)
bwc50 = (50, 50, 50)
bwc51 = (51, 51, 51)
bwc52 = (52, 52, 52)
bwc53 = (53, 53, 53)
bwc54 = (54, 54, 54)
bwc55 = (55, 55, 55)
bwc56 = (56, 56, 56)
bwc57 = (57, 57, 57)
bwc58 = (58, 58, 58)
bwc59 = (59, 59, 59)
bwc60 = (60, 60, 60)
bwc61 = (61, 61, 61)
bwc62 = (62, 62, 62)
bwc63 = (63, 63, 63)
bwc64 = (64, 64, 64)
bwc65 = (65, 65, 65)
bwc66 = (66, 66, 66)
bwc67 = (67, 67, 67)
bwc68 = (68, 68, 68)
bwc69 = (69, 69, 69)
bwc70 = (70, 70, 70)
bwc71 = (71, 71, 71)
bwc72 = (72, 72, 72)
bwc73 = (73, 73, 73)
bwc74 = (74, 74, 74)
bwc75 = (75, 75, 75)
bwc76 = (76, 76, 76)
bwc77 = (77, 77, 77)
bwc78 = (78, 78, 78)
bwc79 = (79, 79, 79)
bwc80 = (80, 80, 80)
bwc81 = (81, 81, 81)
bwc82 = (82, 82, 82)
bwc83 = (83, 83, 83)
bwc84 = (84, 84, 84)
bwc85 = (85, 85, 85)
bwc86 = (86, 86, 86)
bwc87 = (87, 87, 87)
bwc88 = (88, 88, 88)
bwc89 = (89, 89, 89)
bwc90 = (90, 90, 90)
bwc91 = (91, 91, 91)
bwc92 = (92, 92, 92)
bwc93 = (93, 93, 93)
bwc94 = (94, 94, 94)
bwc95 = (95, 95, 95)
bwc96 = (96, 96, 96)
bwc97 = (97, 97, 97)
bwc98 = (98, 98, 98)
bwc99 = (99, 99, 99)
bwc100 = (100, 100, 100)
bwc101 = (101, 101, 101)
bwc102 = (102, 102, 102)
bwc103 = (103, 103, 103)
bwc104 = (104, 104, 104)
bwc105 = (105, 105, 105)
bwc106 = (106, 106, 106)
bwc107 = (107, 107, 107)
bwc108 = (108, 108, 108)
bwc109 = (109, 109, 109)
bwc110 = (110, 110, 110)
bwc111 = (111, 111, 111)
bwc112 = (112, 112, 112)
bwc113 = (113, 113, 113)
bwc114 = (114, 114, 114)
bwc115 = (115, 115, 115)
bwc116 = (116, 116, 116)
bwc117 = (117, 117, 117)
bwc118 = (118, 118, 118)
bwc119 = (119, 119, 119)
bwc120 = (120, 120, 120)
bwc121 = (121, 121, 121)
bwc122 = (122, 122, 122)
bwc123 = (123, 123, 123)
bwc124 = (124, 124, 124)
bwc125 = (125, 125, 125)
bwc126 = (126, 126, 126)
bwc127 = (127, 127, 127)
bwc128 = (128, 128, 128)
bwc129 = (129, 129, 129)
bwc130 = (130, 130, 130)
bwc131 = (131, 131, 131)
bwc132 = (132, 132, 132)
bwc133 = (133, 133, 133)
bwc134 = (134, 134, 134)
bwc135 = (135, 135, 135)
bwc136 = (136, 136, 136)
bwc137 = (137, 137, 137)
bwc138 = (138, 138, 138)
bwc139 = (139, 139, 139)
bwc140 = (140, 140, 140)
bwc141 = (141, 141, 141)
bwc142 = (142, 142, 142)
bwc143 = (143, 143, 143)
bwc144 = (144, 144, 144)
bwc145 = (145, 145, 145)
bwc146 = (146, 146, 146)
bwc147 = (147, 147, 147)
bwc148 = (148, 148, 148)
bwc149 = (149, 149, 149)
bwc150 = (150, 150, 150)
bwc151 = (151, 151, 151)
bwc152 = (152, 152, 152)
bwc153 = (153, 153, 153)
bwc154 = (154, 154, 154)
bwc155 = (155, 155, 155)
bwc156 = (156, 156, 156)
bwc157 = (157, 157, 157)
bwc158 = (158, 158, 158)
bwc159 = (159, 159, 159)
bwc160 = (160, 160, 160)
bwc161 = (161, 161, 161)
bwc162 = (162, 162, 162)
bwc163 = (163, 163, 163)
bwc164 = (164, 164, 164)
bwc165 = (165, 165, 165)
bwc166 = (166, 166, 166)
bwc167 = (167, 167, 167)
bwc168 = (168, 168, 168)
bwc169 = (169, 169, 169)
bwc170 = (170, 170, 170)
bwc171 = (171, 171, 171)
bwc172 = (172, 172, 172)
bwc173 = (173, 173, 173)
bwc174 = (174, 174, 174)
bwc175 = (175, 175, 175)
bwc176 = (176, 176, 176)
bwc177 = (177, 177, 177)
bwc178 = (178, 178, 178)
bwc179 = (179, 179, 179)
bwc180 = (180, 180, 180)
bwc181 = (181, 181, 181)
bwc182 = (182, 182, 182)
bwc183 = (183, 183, 183)
bwc184 = (184, 184, 184)
bwc185 = (185, 185, 185)
bwc186 = (186, 186, 186)
bwc187 = (187, 187, 187)
bwc188 = (188, 188, 188)
bwc189 = (189, 189, 189)
bwc190 = (190, 190, 190)
bwc191 = (191, 191, 191)
bwc192 = (192, 192, 192)
bwc193 = (193, 193, 193)
bwc194 = (194, 194, 194)
bwc195 = (195, 195, 195)
bwc196 = (196, 196, 196)
bwc197 = (197, 197, 197)
bwc198 = (198, 198, 198)
bwc199 = (199, 199, 199)
bwc200 = (200, 200, 200)
bwc201 = (201, 201, 201)
bwc202 = (202, 202, 202)
bwc203 = (203, 203, 203)
bwc204 = (204, 204, 204)
bwc205 = (205, 205, 205)
bwc206 = (206, 206, 206)
bwc207 = (207, 207, 207)
bwc208 = (208, 208, 208)
bwc209 = (209, 209, 209)
bwc210 = (210, 210, 210)
bwc211 = (211, 211, 211)
bwc212 = (212, 212, 212)
bwc213 = (213, 213, 213)
bwc214 = (214, 214, 214)
bwc215 = (215, 215, 215)
bwc216 = (216, 216, 216)
bwc217 = (217, 217, 217)
bwc218 = (218, 218, 218)
bwc219 = (219, 219, 219)
bwc220 = (220, 220, 220)
bwc221 = (221, 221, 221)
bwc222 = (222, 222, 222)
bwc223 = (223, 223, 223)
bwc224 = (224, 224, 224)
bwc225 = (225, 225, 225)
bwc226 = (226, 226, 226)
bwc227 = (227, 227, 227)
bwc228 = (228, 228, 228)
bwc229 = (229, 229, 229)
bwc230 = (230, 230, 230)
bwc231 = (231, 231, 231)
bwc232 = (232, 232, 232)
bwc233 = (233, 233, 233)
bwc234 = (234, 234, 234)
bwc235 = (235, 235, 235)
bwc236 = (236, 236, 236)
bwc237 = (237, 237, 237)
bwc238 = (238, 238, 238)
bwc239 = (239, 239, 239)
bwc240 = (240, 240, 240)
bwc241 = (241, 241, 241)
bwc242 = (242, 242, 242)
bwc243 = (243, 243, 243)
bwc244 = (244, 244, 244)
bwc245 = (245, 245, 245)
bwc246 = (246, 246, 246)
bwc247 = (247, 247, 247)
bwc248 = (248, 248, 248)
bwc249 = (249, 249, 249)
bwc250 = (250, 250, 250)
bwc251 = (251, 251, 251)
bwc252 = (252, 252, 252)
bwc253 = (253, 253, 253)
bwc254 = (254, 254, 254)
bwc255 = (255, 255, 255)
black = (0, 0, 0)
white = (255, 255, 255)
red = (255, 0, 0)
dark_red = (127, 0, 0)
yellow = (0, 255, 0)
blue = (0, 0, 255)
red_orange = (255, 127, 80)
green = (0, 255, 255)
purple = (255, 0, 255)
答案 0 :(得分:0)
您可以使用元编程:
class GlobalNamespace(type):
def __init__(cls, name, bases, dct):
type.__init__(cls, name, bases, dct)
globals().update({n: v for n, v in dct.items() if not n.startswith("__")})
class RGBcolors(metaclass=GlobalNamespace):
...
然后,导入包含RGBcolors的模块
注意:请阅读您的问题和此答案的评论,解释为什么原则上这是一个坏主意。不要将类视为模块。它们是两个不同的概念。