如何打印具有用户定义属性的类的实例?

时间:2013-05-30 04:21:14

标签: python printing instance python-3.3 user-defined

我正在试图弄清楚如何在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()

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

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

1 个答案:

答案 0 :(得分: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())
相关问题