繁体   English   中英

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

[英]How can I print instance of class that has user defined attributes?

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

似乎您没有在调用方法。

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())

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM