码迷,mamicode.com
首页 > 其他好文 > 详细

property getitem setitem

时间:2017-11-18 18:55:24      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:recent   file   setter   obj   def   back   port   foo   import   

 

 

 

 

 

。。。。。

>>> class Foo(object):
    def __init__(self,name):
        self._name = name
    @property
    def get_name(self):
        return self._name

    
>>> f1=Foo(yuyang)
>>> f1.get_name
yuyang
>>> f1.get_name = dfd
Traceback (most recent call last):
  File "<pyshell#10>", line 1, in <module>
    f1.get_name = dfd
AttributeError: cant set attribute

 

大概VC

>>> class Foo(object):
    def __init__(self,name):
        self._name = name
    @property
    def get_name(self):
        return self._name
    @get_name.setter
    def get_name(self,value):
        self._name = value

        
>>> f2=Foo(yuyagng)
>>> f2.get_name
yuyagng
>>> f2.get_name = alex
>>> f2.get_name
alex

收费电视

>>> class Foo(object):
    def __init__(self,name):
        self._name = name
    def get_name(self):
        return self._name
    def set_name(self,value):
        self._name = value
    property(get_name,set_name)

    
>>> f3 = Foo(dsadsa)
>>> f3.get_name
<bound method Foo.get_name of <__main__.Foo object at 0x0000000002F13EF0>>
>>> f3.get_name()
dsadsa
>>> f3.set_name(wupeiqi)
>>> f3.get_name()
wupeiqi
>>> class Foo(object):
    def __init__(self,name):
        self._name = name
    def get_name(self):
        return self._name
    def set_name(self,value):
        self._name = value
    name = property(get_name,set_name)

    
>>> f3 = Foo(dsadsa)
>>> f3.get_name()
dsadsa
>>> f3.name = laoda
>>> f3.get_name()
laoda

VC下

vc

>>> class Mycalss(object):
    d={a:1,b:2,c:3}
    def __getitem__(self,value):
        return self.d[value]
    def __setitem__(self,key,value):
        self.d[key] = value

        
>>> myclass = Myclass()
Traceback (most recent call last):
  File "<pyshell#23>", line 1, in <module>
    myclass = Myclass()
NameError: name Myclass is not defined
>>> myclass = Mycalss()
>>> myclass[a]
1
>>> myclass[b]
2
>>> myclass[f]=4
>>> myclass[f]
4
>>> myclass[a]=aaaa
>>> myclass[a]
aaaa

 

import base64


>>> a= asdasdfas
>>> a2 =a.encode(utf-8)
>>> a64 = base64.b64encode(a2)
>>> a2
basdasdfas
>>> a64
bYXNkYXNkZmFz
>>> base64.b64encode(a64)
bWVhOa1lYTmtabUZ6
>>> a64
bYXNkYXNkZmFz
>>> base64.b64decode(a64)
basdasdfas
>>> a64.decode(utf-8)
YXNkYXNkZmFz
>>> a64_de = base64.b64decode(a64)
>>> a64_de
basdasdfas

 

property getitem setitem

标签:recent   file   setter   obj   def   back   port   foo   import   

原文地址:http://www.cnblogs.com/yuyang26/p/7857463.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!