object and namespace

http://effbot.org/zone/python-objects.htm

几点总结:

(1) 类的基本属性

      . id, returned by id(obj)

      . type, returned by type(obj)

      . some content

      . methods; some objects have methods to change the content, and some don’t; eg, list vs. turple

      . names

(2) namespace

      . namespace are pairs of (name, object reference). eg., n=10 where ‘n’ is the name and ‘10’ is the int object.

      . names are not really properties of objects

(3) assignment

      举例说明。

     name = 10

     name = 20

     . add name ‘name’ to local namespace, and refer it to an integer object containing value 10

     . refer name to another integer object containing value 20

     . the original object ‘10’ is not affected by this operation and it doesn’t care

     再举一个object可变的例子。

     name = []

     name.append(a)

     . add name ‘name’ to a local namespace, and refer it to an empty list object; this modifies the namespace

     . an object method is called; this modifies the content of the the list object, but it doesn’t touch the namespace

(4) note

      Things like name.attr and name[index] are just syntactic sugar for method calls.

      The first corresponds to __setattr__/__getattr__, the second to __setitem__/__getitem__ (depending on which side of the assignment they appear).

      --> syntactic suger是指为了更容易阅读而设计出来的编程语言的语法

posted on 2017-08-04 15:21  freshair_cn  阅读(179)  评论(0编辑  收藏  举报

导航