在“ python”中从另一个类调用变量

时间:2018-07-06 11:44:19

标签: python python-3.x


先生,你好 我在从类JB()和KL()调用可变日期组时遇到问题,因为我想比较JB和KL中的组是否为1且JB()== KL()的日期,我想打印日期。我已经尝试过该论坛的一些解决方案,但似乎失败了。

{{1}}

2 个答案:

答案 0 :(得分:0)

您可能会遇到错误,因为您尝试创建实例clashJB和clashKL而不传递必需的参数date,times,group。您需要将其创建为:

clashJB = JB(<date>, <timeslot>, <group>)
clashKL = KL(<date>, <timeslot>, <group>)

执行比较操作之前。

如果对类定义及其使用有任何疑问,可以通过https://docs.python.org/2/tutorial/classes.html进行检查。

答案 1 :(得分:0)

以下代码按您的期望工作。

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)