improve your python code(8)__python

來源:互聯網
上載者:User
1. [] {} ().用列表解析器代替for遍曆

理由:
高校 2. 函數傳參既非傳引用也非傳值

a = 5   # 開闢一個記憶體存放5,將a這個標籤貼在這個記憶體上b = a   # 將b這個標籤也貼在剛才的那個記憶體上b = 7   # 開闢一個記憶體存放7,將b這個標籤貼在這個記憶體上,剛才的那個b被覆蓋。


3. *args,**kwargs



4. staticmethod和classmethod

首先要明白,什麼是執行個體方法、靜態方法和類方法:

class Demo(object):    def instance_method(self, your_para):        """        this is an instance_method        you should call it like the follow:        a = Demo()        a.instance_method(your_para)        plus: in python, we denote 'cls' as latent para of Class        while 'self' as latent para of the instance of the Class        :param your_para:         :return:         """        print("call instance_method and get:", your_para)    @classmethod    def class_method(cls, your_para):        """        this is a class_method        you can call it like the follow:        method1:        a = Demo()        a.class_method(your_para)        method2:        Demo.class_method        plus: in python, we denote 'cls' as latent para of Class        while 'self' as latent para of the instance of the Class        :param your_para:         :return:         """        print("call class_method and get:", your_para)    @staticmethod    def static_method(your_para):        """        this is a static_method and you can call it like the         methods of class_method        :param your_para:         :return:         """        print("call static_method and get:", your_para)
雖然類方法在調用的時候沒有顯式聲明cls,但實際上類本身是作為隱含參數傳入的。這就像執行個體方法在調用的時候也沒有顯式聲明self,但實際上執行個體本身是作為隱含參數傳入的。 對於靜態函數,我們一般把與類無關也與執行個體無關的函數定義為靜態函數。例如入口檢查的函數就最好定義成靜態函數。 類方法的妙處:
1,在繼承中的作用:
class Fruit(object):    total = 0   # 這是一個類屬性    @classmethod    def print_total(cls):        print('this is the ', cls, '.total:', cls.total, ' and its id: ', id(cls.total))    # cls是類本身,列印類屬性total的值        print('this is the Fruit.total:', Fruit.total, 'and its id: ', id(Fruit.total))        print("=======================")    @classmethod    def set(cls, value):        cls.total = valueclass Apple(Fruit):    passclass Orange(Fruit):    passapp1 = Apple()app1.set(10)app1.print_total()Apple.print_total()Fruit.set(2)app1.print_total()Fruit.print_total()"""output:this is the  <class '__main__.Apple'> .total: 10  and its id:  1355201264this is the Fruit.total: 0 and its id:  1355200944=======================this is the  <class '__main__.Apple'> .total: 10  and its id:  1355201264this is the Fruit.total: 0 and its id:  1355200944=======================this is the  <class '__main__.Apple'> .total: 10  and its id:  1355201264this is the Fruit.total: 2 and its id:  1355201008=======================this is the  <class '__main__.Fruit'> .total: 2  and its id:  1355201008this is the Fruit.total: 2 and its id:  1355201008======================="""
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.