python在子类中添加新的属性_向Python对象添加新属性?

To implement user-defined objects, I settled on the simplest possible

design; a scheme where objects were represented by a new kind of

built-in object that stored a class reference pointing to a "class

object" shared by all instances of the same class, and a dictionary,

dubbed the "instance dictionary", that contained the instance

variables.

In this implementation, the instance dictionary would contain the

instance variables of each individual object whereas the class object

would contain stuff shared between all instances of the same class in

particular, methods. In implementing class objects, I again chose the

simplest possible design; the set of methods of a class were stored in

a dictionary whose keys are the method names. This, I dubbed the class

dictionary. To support inheritance, class objects would additionally

store a reference to the class objects corresponding to the base

classes. At the time, I was fairly naïve about classes, but I knew

about multiple inheritance, which had recently been added to C++. I

decided that as long as I was going to support inheritance, I might as

well support a simple-minded version of multiple inheritance. Thus,

every class object could have one or more base classes.

In this implementation, the underlying mechanics of working with

objects are actually very simple. Whenever changes are made to

instance or class variables, those changes are simply reflected in the

underlying dictionary object. For example, setting an instance

variable on an instance updates its local instance dictionary.

Likewise, when looking up the value of a instance variable of an

object, one merely checks its instance dictionary for the existence of

that variable. If the variable is not found there, things become a

little more interesting. In that case, lookups are performed in the

class dictionary and then in the class dictionaries of each of the

base classes.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值