[英]Calling variable from another class in “python”
早上好,先生,我在从类JB()和KL()调用可变日期组时遇到问题,因为我想比较JB和KL中的组是否为1且JB()== KL()的日期,我想打印日期。 我已经尝试过该论坛的一些解决方案,但看来我失败了。
class JB(object):
def __init__(self, date, timeslot, group):
self.date = date
self.timeslot = timeslot
self.group = group
def timetableJB(self):
print(self.date)
tt1 = JB("8 Sept", (TS3_JB, TS4_JB, TS5_JB, TS6_JB), G1)
tt2 = JB("15 Sept", (TS3_JB, TS4_JB, TS5_JB, TS6_JB), G2)
tt3 = JB("22 Sept", (TS3_JB, TS4_JB, TS5_JB, TS6_JB), G3)
class KL(object):
def __init__(self, date, timeslot, group):
self.date = date
self.timeslot = timeslot
self.group = group
def timetableKL(self):
print(self.date, self.timeslot, self.group)
tt2 = KL("15 Sept", (TS1_KL, TS2_KL, TS3_KL), G1)
tt3_1 = KL("22 Sept", (TS1_KL, TS2_KL, TS3_KL), G2)
tt3_2 = KL("23 Sept", (TS4_KL, TS5_KL, TS6_KL), G3)
class PP(object):
def __init__(self, date, timeslot, group):
self.date = date
self.timeslot = timeslot
self.group = group
def timetablePP(self):
print(self.date, self.timeslot, self.group)
tt3 = PP("22 Sept", (TS1_PP, TS2_PP, TS3_PP), G1)
tt4_1 = PP("29 Sept", (TS1_PP, TS2_PP, TS3_PP), G2)
tt4_2 = PP("30 Sept", (TS4_PP, TS5_PP, TS6_PP), G3)
print("")
print("Clash analysis for timetable KL and JB")
clashJB = JB()<----- I have error in this part
clashKL = KL()<----- I have error in this part
while clashJB.group and clashKL.group == G1:
if clashJB.date == clashKL.date:
print(clashJB.date)
您可能会收到错误消息,因为您尝试创建实例clashJB和clashKL而不传递必需参数date,times,group。 您需要将其创建为:
clashJB = JB(<date>, <timeslot>, <group>)
clashKL = KL(<date>, <timeslot>, <group>)
在执行比较操作之前。
如果您对类定义及其使用有任何疑问,可以浏览https://docs.python.org/2/tutorial/classes.html 。
以下代码按照您的期望工作。
class JB(object):
def __init__(self, date, timeslot, group):
self.date = date
self.timeslot = timeslot
self.group = group
def timetableJB(self):
print(self.date)
class KL(object):
def __init__(self, date, timeslot, group):
self.date = date
self.timeslot = timeslot
self.group = group
def timetableKL(self):`enter code here`
print(self.date, self.timeslot, self.group)
class PP(object):
def __init__(self, date, timeslot, group):
self.date = date
self.timeslot = timeslot
self.group = group
def timetablePP(self):
print(self.date, self.timeslot, self.group)
clashJB = JB(1,2,'G1')<---- Note the change
clashKL = KL(1,2,'G1')<-----Note the change
while clashJB.group and clashKL.group == 'G1':
if clashJB.date == clashKL.date:
print(clashJB.date)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.