# -*- coding: utf-8 -*-
"""
invoke的作用。property(fget=None, fset=None, fdel=None, doc=None) -> property attribute
要么加上@decorator(其实也是invoke),要么通过property进行invoke
这里要增加理解的是type问题。
1. All data is represented by objects or by relations between objects. Every object has an identity, a type and a value.
This "everything is an object" model is backed by the CPython implementation.
2. Lists are objects. 42 is an object. Modules are objects. Functions are objects. Python bytecode is also kept in an object.
要么是objects,要么是references
3. An object’s type determines the operations that the object supports (e.g., “does it have a length?”) and also defines the possible values for objects of that type.
type才是判断objects的本质,某个object是function类的,是code类的--即是type。
type得到的结果 <class 'int'> <class 'type'> <class '__main__.Joe'> <class 'function'> <class 'code'> <type 'property'>
4. a class is a mechanism Python gives us to create new user-defined types from Python code.
The terms "class" and "type" are an example of two names referring to the same concept.
"""
class Accout(object):
def __init__(self):
self._acct_num = None
def get_acct_num(self):
return self._acct_num
def set_acct_num(self, value):
self._acct_num = value
def del_acct_num(self):
del self._acct_num
acct_num = property(get_acct_num, set_acct_num, del_acct_num, "Account number property.")
ac = Accout()
print "hey, the test of Accout",ac._acct_num==ac.acct_num
print ac.acct_num
ac.acct_num = 2
print ac._acct_num
print '--------------------------------'
class C(object):
def __init__(self):
self._x = None
@property
# the x property. the decorator creates a read-only property
def x(self):
return self._x
@x.setter
# the x property setter makes the property writeable
def x(self, value):
self._x = value
@x.deleter #这里注释掉之后,c.x成为一个method而
def x(self):
del self._x
c = C()
c.x = 3 #将delete注释掉,则这里的setter也不成功,相当于重设了一个变量为c.x
print c._x
print '--------------------------------'
print type(c).__dict__['x'].__get__(c,type(c))
#del c.x#将x与c断掉关联,但是type(c)的__dict__肯定是仍有x
print c.x
print c._x
print '--------------------------------'
print type(c).__dict__.items()
print type(c).__dict__['x']
print type(c).__dict__['x'].__get__(c,type(c))
print type(C).__dict__.items()
print type(C),type(c),type(Accout),type(object),type(C.x)
#print class(C),class(c),class(Accout),class(object),class(C.x)
print c.x == c._x
网友评论