property getitem setitem

Posted dragonbird

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了property getitem setitem相关的知识,希望对你有一定的参考价值。

 

 

 

 

 

。。。。。

>>> 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的主要内容,如果未能解决你的问题,请参考以下文章

\_\_setitem\_\_和\_\_getitem和\_\_delitem__

GETATTR,DELATTR,SETATTR与GETITEM,SETITEM,DELITEM区别

__setitem__,__getitem,__delitem__

python学习之__getitem__,__setitem__,__delitem__

__getitem__ __setitem__ __delitem__ 使用

python - __setitem__/__getitem__/__delitem__类的内置方法