Python中一切都是对象。类提供了创建新类型对象的机制。这篇教程中,我们不谈类和面向对象的基本知识,而专注在更好地理解Python面向对象编程上。假设我们使用新风格的python类,它们继承自object父类。
定义类
class 语句可以定义一系列的属性、变量、方法,他们被该类的实例对象所共享
。下面给出一个简单类定义:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
class Account(object): num_accounts = 0 def __init__(self, name, balance): self.name = name self.balance = balance Account.num_accounts += 1 def del_account(self): Account.num_accounts -= 1 def deposit(self, amt): self.balance = self.balance + amt def withdraw(self, amt): self.balance = self.balance - amt def inquiry(self): return self.balance |
类定义引入了以下新对象:
- 类对象
- 实例对象
- 方法对象
类对象
程序执行过程中遇到类定义时,就会创建新的命名空间,命名空间包含所有类变量和方法定义的名称绑定。注意该命名空间并没有创建类方法可以使用的新局部作用域,因此在方法中访问变量需要全限定名称。上一节的Account
类演示了该特性;尝试访问num_of_accounts
变量的方法需要使用全限定名称Account.num_of_accounts
,否则,如果没有在__init__
方法中使用全限定名称,会引发如下错误:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
class Account(object): num_accounts = 0 def __init__(self, name, balance): self.name = name self.balance = balance num_accounts += 1 def del_account(self): Account.num_accounts -= 1 def deposit(self, amt): self.balance = self.balance + amt def withdraw(self, amt): self.balance = self.balance - amt def inquiry(self): return self.balance >>> acct = Account('obi', 10) Traceback (most recent call last): File "python", line 1, in <module> File "python", line 9, in __init__ UnboundLocalError: local variable 'num_accounts' referenced before assignment |
类定义执行的最后,会创建一个类对象。在进入类定义之前有效的那个作用域现在被恢复了,同时类对象被绑定到类定义头的类名上。
先偏离下话题,你可能会问如果创建的类是对象,那么类对象的类是什么呢?。与一切都是对象的python哲学一致,类对象确实有个类,即python新风格类中的type
类。
1 2 |
>>> type(Account) <class 'type'> |
让你更迷惑一点,Account类型的类型是type。type类是个元类,用于创建其他类,我们稍后教程中再介绍。
类对象支持属性引用和实例化。属性通过标准的点语法引用,即对象后跟句点,然后是属性名:obj.name。有效的属性名是类对象创建后类命名空间中出现的所有变量和方法名。例如:
1 2 3 4 |
>>> Account.num_accounts >>> 0 >>> Account.deposit >>> <unbound method Account.deposit> |
类实例化使用函数表示法。实例化会像普通函数一样无参数调用类对象,如下文中的Account类:
类实例化使用函数表示法。实例化会像普通函数一样无参数调用类对象,如下文中的Account类:
Python中一切都是对象。类提供了创建新类型对象的机制。这篇教程中,我们不谈类和面向对象的基本知识,而专注在更好地理解Python面向对象编程上。假设我们使用新风格的python类,它们继承自object父类。
定义类
class 语句可以定义一系列的属性、变量、方法,他们被该类的实例对象所共享
。下面给出一个简单类定义:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
class Account(object): num_accounts = 0 def __init__(self, name, balance): self.name = name self.balance = balance Account.num_accounts += 1 def del_account(self): Account.num_accounts -= 1 def deposit(self, amt): self.balance = self.balance + amt def withdraw(self, amt): self.balance = self.balance - amt def inquiry(self): return self.balance |
类定义引入了以下新对象:
- 类对象
- 实例对象
- 方法对象
类对象
程序执行过程中遇到类定义时,就会创建新的命名空间,命名空间包含所有类变量和方法定义的名称绑定。注意该命名空间并没有创建类方法可以使用的新局部作用域,因此在方法中访问变量需要全限定名称。上一节的Account
类演示了该特性;尝试访问num_of_accounts
变量的方法需要使用全限定名称Account.num_of_accounts
,否则,如果没有在__init__
方法中使用全限定名称,会引发如下错误:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
class Account(object): num_accounts = 0 def __init__(self, name, balance): self.name = name self.balance = balance num_accounts += 1 def del_account(self): Account.num_accounts -= 1 def deposit(self, amt): self.balance = self.balance + amt def withdraw(self, amt): self.balance = self.balance - amt def inquiry(self): return self.balance >>> acct = Account('obi', 10) Traceback (most recent call last): File "python", line 1, in <module> File "python", line 9, in __init__ UnboundLocalError: local variable 'num_accounts' referenced before assignment |
类定义执行的最后,会创建一个类对象。在进入类定义之前有效的那个作用域现在被恢复了,同时类对象被绑定到类定义头的类名上。
先偏离下话题,你可能会问如果创建的类是对象,那么类对象的类是什么呢?。与一切都是对象的python哲学一致,类对象确实有个类,即python新风格类中的type
类。
1 2 |
>>> type(Account) <class 'type'> |
让你更迷惑一点,Account类型的类型是type。type类是个元类,用于创建其他类,我们稍后教程中再介绍。
类对象支持属性引用和实例化。属性通过标准的点语法引用,即对象后跟句点,然后是属性名:obj.name。有效的属性名是类对象创建后类命名空间中出现的所有变量和方法名。例如:
1 2 3 4 |
>>> Account.num_accounts >>> 0 >>> Account.deposit >>> <unbound method Account.deposit> |
类实例化使用函数表示法。实例化会像普通函数一样无参数调用类对象,如下文中的Account类: