Python的class中的参数的用法问题
高洛峰
高洛峰 2017-04-17 14:49:54
0
2
347

在 codecademy 学习 Python 时遇到的问题,创建了两个类EmployeeCEO

class Employee(object):
    def __init__(self, name):
        self.name = name
    def greet(self, other):
        print "Hello, %s" % other.name

class CEO(Employee):
    def greet(self, other):
        print "Get back to work, %s!" % other.name

ceo = CEO("Emily")
emp = Employee("Steve")
emp.greet(ceo)  # Hello, Emily
ceo.greet(emp)  # Get back to work, Steve!

这里为什么会有other.name这种用法,是什么意思?

self.name = name理解为当前对象的成员变量name赋值为name,是不是说self就是实例,name就是它的一个属性?

那么other.name是什么意思呢?

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(2)
刘奇

Huh? It's very simple. 1. There is a name attribute (member variable) in the other parameter. 2.self points to the object itself instantiated by the class.

Peter_Zhu

Other is an instance object parameter. This instance object has the attribute name. If the object you pass in is not an instance of CEO or Employee, an exception AttributeError will be reported.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!