属性访问

__getattr__(self,name)

__getattribute__(self,name)

__setattr__(self,name,value)

__delattr__(self,name)

class C:    def __getattribute__(self,name):#当该类的属性被访问的行为        print('getattribute')        return super().__getattribute__(name)        def __setattr__(self,name,value):#当一个属性被设置时        print('setattr')        super().__setattr__(name,value)            def __delattr__(self,value):#当一个属性被删除        print('delattr')        super().__delattr__(name)            def __getattr__(self,name):#当用户获取一个不存在的上述属性        print('getattr')

>>> c = C()>>> c.xgetattributegetattr>>> c.x = 1setattr>>> c.xgetattribute1>>> del c.xdelattr

class Rectangle:    def __init__(self,width = 0,height = 0):        self.width = width        self.height = height    def __setattr__(self,name,value):        if name == 'square':            self.width = value            self.height = height        else:            self.name = value    def getArea(self):        return self.width * self.height

class Rectangle:    def __init__(self,width = 0,height = 0):        self.width = width        self.height = height    def __setattr__(self,name,value):        if name == 'square':            self.width = value            self.height = height        else:            self.__dict__[name] = value    def getArea(self):        return self.width * self.height

>>> r1 = Rectangle(4,5)>>> r1.getArea()20

>>> for i in 'tianjun':...     print(i)...tianjun>>>

Python有两个BIF

iter()

next()

class Fibs:      def __init__(self,n = 10):          self.a = 0          self.b = 1          self.n = n      def __iter__(self):         return self           def __next__(self):                 self.a,self.b = self.b,self.a+self.b         if self.a > self.n:             raise StopIteration         return self.a

def fibonacci(n):     a, b, counter = 0, 1, 0    while True:        if (counter > n):             return        yield a        a, b = b, a + b        counter += 1f = fibonacci(10) # f 是一个迭代器，由生成器返回生成 while True:    try:        print (next(f), end=" ")    except StopIteration:        sys.exit()

0 1 1 2 3 5 8 13 21 34 55

104 篇文章28 人订阅

我来说两句

0 条评论
{"ft_lang_label":"__label__zh","ft_lang_prob":0.5792504,"math_prob":0.98509717,"size":2596,"snap":"2019-43-2019-47","text_gpt3_token_len":1386,"char_repetition_ratio":0.16435185,"word_repetition_ratio":0.2231405,"special_character_ratio":0.28235748,"punctuation_ratio":0.22149123,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9964734,"pos_list":[0,1,2],"im_url_duplicate_count":[null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-10-18T21:10:32Z\",\"WARC-Record-ID\":\"<urn:uuid:fa2b48fa-ed0f-4d20-8d56-8aed366a49bb>\",\"Content-Length\":\"53856\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:136c406a-169b-410e-b0fc-1f49b3a60ce9>\",\"WARC-Concurrent-To\":\"<urn:uuid:5ebaa288-1359-45fb-8892-c7ec1827de5a>\",\"WARC-IP-Address\":\"119.28.39.127\",\"WARC-Target-URI\":\"https://cloud.tencent.com/developer/article/1474308\",\"WARC-Payload-Digest\":\"sha1:BIA3JLE7EVPYEOQSLC6GTEMM5XJIS6UX\",\"WARC-Block-Digest\":\"sha1:HULCQWYX375RRDBG326STZQOWGXOT3T3\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-43/CC-MAIN-2019-43_segments_1570986684854.67_warc_CC-MAIN-20191018204336-20191018231836-00477.warc.gz\"}"}