1

我试图弄清楚如何在 Python 3 中打印出具有用户定义属性的类的实例。这是我拥有的代码:

class Attendie: 
    def __init__(self, fname, lname, company, state, email):
        self.fname = fname
        self.lname = lname
        self.company = company
        self.state = state
        self.email = email

    def getFname(self):
        return self.fname

    def getLname(self):
        return self.lname

    def getCompany(self):
        return self.company

    def getState(self):
        return self.state

    def getEmail(self):
        return self.email

def main():


    fname = input("what is the attendie's first name? ")
    lname = input("What is the attendie's last name? ")
    company = input("What company is the attendie with? ")
    state = input("What state is the attendie from? ")
    email = input("What is the attendie's email address? ")

    Person = Attendie(fname, lname, company, state, email)

    print(Person.getFname)
    print(Person.getLname)
    print(Person.getCompany)
    print(Person.getState)
    print(Person.getEmail)

if __name__ == '__main__': main()

运行程序后,我收到这些类型的错误消息。

< main .Attendie 对象的绑定方法 Attendie.getFname位于 0x00000000031DCDD8

4

1 回答 1

2

看来您没有调用这些方法。

In [1]: class A(object):
   ...:     def __init__(self, n):
   ...:         self.n = n
   ...:     def get_n(self):
   ...:         return self.n
   ...:     

In [2]: a = A(5)

In [3]: a.get_n
Out[3]: <bound method A.get_n of <__main__.A object at 0xa56516c>>

In [4]: a.get_n()
Out[4]: 5

如下更改您的代码应该可以修复它:

print(Person.getFname())
print(Person.getLname())
print(Person.getCompany())
print(Person.getState())
print(Person.getEmail())
于 2013-05-30T04:57:18.627 回答