accessibility

# -*- coding: utf-8 -*-
"""
invoke的作用。property(fget=None, fset=None, fdel=None, doc=None) -> property attribute
要么加上@decorator(其實(shí)也是invoke),要么通過property進(jìn)行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的本質(zhì),某個(gè)object是function類的,是code類的--即是type。
type得到的結(jié)果 <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成為一個(gè)method而
    def x(self):
        del self._x

c = C()
c.x = 3 #將delete注釋掉,則這里的setter也不成功,相當(dāng)于重設(shè)了一個(gè)變量為c.x
print c._x
print '--------------------------------'
print type(c).__dict__['x'].__get__(c,type(c))
#del c.x#將x與c斷掉關(guān)聯(lián),但是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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容