打印课程

时间:2017-12-09 13:43:45

标签: python class output

您好我正在尝试使用课程打印4个乐队成员的详细信息,但是我只能打印一个,生病的样本给你一个理解,这是我的整个代码

class Person:
    def __init__(self, _firstname, _surname, d, m, y):
        self._firstname = _firstname
        self._surname = _surname
        self.d = d
        self.m = m
        self.y = y

    def __repr__(self):
        return '{} {} \n{} {} {}'.format(john._firstname, john._surname, john.d, john.m, john.y)
        return '{} {} \n{} {} {}'.format(paul._firstname, paul._surname,     paul.d, paul.m, paul.y)
        return '{} {} \n{} {} {}'.format(george._firstname,     george._surname, george.d, george.m, george.y)
        return '{} {} \n{} {} {}'.format(ringo._firstname, ringo._surname,     ringo.d, ringo.m, ringo.y)

john = Person('John','Lennon','8th','October','1940')
paul = Person('Paul','McCartney','18th','June','1942')
george = Person('George','Harrison','25th','February','1943')
ringo = Person('Ringo','Starr','7th','July','1940')


john._firstname = 'John'
john._surname = 'Lennon'
john.d = '8th'
john.m = 'October'
john.y = '1940'


paul._firstname = 'Paul'
paul._surname = 'McCartney'
paul._d = '18th'
paul._m = 'June'
paul._y = '1942'

george._firstname = 'George'
george._surname = 'Harrison'
george._d = '25th'
george._m = 'February'
george._y = '1943'

ringo._firstname = 'Ringo'
ringo._surname = 'Starr'
ringo._d = '7th'
ringo._m = 'July'
ringo._y = '1940'

print('{} {} \n{} {} {}'.format(john._firstname, john._surname, john.d, john.m, john.y))
print('{} {} \n{} {} {}'.format(paul._firstname, paul._surname, paul.d,     paul.m, paul.y))
print('{} {} \n{} {} {}'.format(george._firstname, george._surname,         george.d, george.m, george.y))
print('{} {} \n{} {} {}'.format(ringo._firstname, ringo._surname,     ringo.d,     ringo.m, ringo.y))

我收到了正确的输出,这是:

John Lennon
8th October 1940
Paul McCartney 
18th June 1942
George Harrison 
25th February 1943
Ringo Starr 
7th July 1940

我希望能够输入:

print(john)
print(george)
print(paul)
print(ringo)

要获得相同的输出,但是当我输入并删除底部的4条打印行时,我得到了这个

John Lennon 
8th October 1940
John Lennon 
8th October 1940
John Lennon 
8th October 1940
John Lennon 
8th October 1940

到目前为止,我的理解是__repr__(self)只能读取一行代码,即john,但这就是我知识停止的地方。

任何帮助都非常感谢!如果您需要更多信息,请问我,我在大学的第一年就是新手,所以感谢您的耐心和帮助!:)

3 个答案:

答案 0 :(得分:2)

任何return命令都会退出该函数,因此在同一级别拥有多个return命令将无法在Python中运行。

您的代码存在的问题是,您应该在self定义中使用关键字__repr__,这样您就可以使用

创建的所有四个实例访问它
john = Person('John','Lennon','8th','October','1940')
paul = Person('Paul','McCartney','18th','June','1942')
george = Person('George','Harrison','25th','February','1943')
ringo = Person('Ringo','Starr','7th','July','1940')

Person实例的创建也会自动存储您输入的所有信息,因此无需为john._firstname等手工指定值。

要给出明确答案:

class Person:
    def __init__(self, _firstname, _surname, d, m, y):
        self._firstname = _firstname
        self._surname = _surname
        self.d = d
        self.m = m
        self.y = y

    def __repr__(self):
        return '{} {} \n{} {} {}'.format(self._firstname,
                                         self._surname,
                                         self.d,
                                         self.m,
                                         self..y)

john = Person('John','Lennon','8th','October','1940')
paul = Person('Paul','McCartney','18th','June','1942')
george = Person('George','Harrison','25th','February','1943')
ringo = Person('Ringo','Starr','7th','July','1940')

print(john)
print(paul)
print(george)
print(ringo)

答案 1 :(得分:0)

您的__repr__讨论类john等类实例。需要编写它以满足任何 Person的实例。

def __repr__(self):
    return '{} {} \n{} {} {}'.format(self._firstname, self._surname, self.d, self.m, self.y)

答案 2 :(得分:-1)

你留下一个带有return语句的函数。以下行被忽略。 在访问属性的方法中,您只使用对象本身的占位符。使用self代替对象的真实姓名。

def __repr__(self): 
  return '{} {} \n{} {} {}'.format(
    self._firstname, self._surname, self.d, self.m, self.y)