python 类内部变量_为什么在Python中从类内部访问类变量需要“self.”?

Instead, I decided to give up on the idea of implicit references to

instance variables. Languages like C++ let you write this->foo to

explicitly reference the instance variable foo (in case there’s a

separate local variable foo). Thus, I decided to make such explicit

references the only way to reference instance variables. In addition,

I decided that rather than making the current object ("this") a

special keyword, I would simply make "this" (or its equivalent) the

first named argument to a method. Instance variables would just always

be referenced as attributes of that argument.

With explicit references, there is no need to have a special syntax

for method definitions nor do you have to worry about complicated

semantics concerning variable lookup. Instead, one simply defines a

function whose first argument corresponds to the instance, which by

convention is named "self." For example:def spam(self,y):

print self.x, y

This approach resembles something I had seen in Modula-3, which had

already provided me with the syntax for import and exception handling.

Modula-3 doesn’t have classes, but it lets you create record types

containing fully typed function pointer members that are initialized

by default to functions defined nearby, and adds syntactic sugar so

that if x is such a record variable, and m is a function pointer

member of that record, initialized to function f, then calling

x.m(args) is equivalent to calling f(x, args). This matches the

typical implementation of objects and methods, and makes it possible

to equate instance variables with attributes of the first argument.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值