2020国产成人精品视频,性做久久久久久久久,亚洲国产成人久久综合一区,亚洲影院天堂中文av色

分享

Python self 參數(shù)詳解

 Four兄 2019-08-31

文章目錄

1、概述

1.1 場(chǎng)景

我們?cè)谑褂?Python 中的 方法 method 時(shí),經(jīng)常會(huì)看到 參數(shù)中帶有 self,但是我們也沒對(duì)這個(gè)參數(shù)進(jìn)行賦值,那么這個(gè)參數(shù)到底是啥意思呢?

2、知識(shí)點(diǎn)

2.1 成員函數(shù)(m) 和 普通方法(f)

  • Python 中的 '類方法' 必須有一個(gè)額外的 第一個(gè)參數(shù)名稱(名稱任意,不過推薦 self),而 '普通方法'則不需要。

  • m、f、c 都是代碼自動(dòng)提示時(shí)的 左邊字母(method、function、class)

# -*- coding: utf-8 -*-class Test(object): def add(self, a, b): # 輸出 a + b print(a + b) def show(self): # 輸出 'Hello World' print('Hello World')def display(a, b): # 輸出 a * b print(a * b)if __name__ == '__main__': test = Test() test.add(1, 2) test.show() display(1, 2)
  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10

  • 11

  • 12

  • 13

  • 14

  • 15

  • 16

  • 17

  • 18

  • 19

  • 20

  • 21

  • 22

  • 23

2.2 類函數(shù),靜態(tài)函數(shù)

  • 類函數(shù)一般用參數(shù) cls

  • 靜態(tài)函數(shù)無法使用 selfcls

class Test(object):    def __init__(self):        print('我是構(gòu)造函數(shù)。。。。')    def foo(self, str):        print(str)    @classmethod    def class_foo(cls, str):        print(str)    @staticmethod    def static_foo(str):        print(str)def show(str):    print(str)if __name__ == '__main__':    test = Test()    test.foo('成員函數(shù)')    Test.class_foo('類函數(shù)')    Test.static_foo('靜態(tài)函數(shù)')    show('普通方法')
  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10

  • 11

  • 12

  • 13

  • 14

  • 15

  • 16

  • 17

  • 18

  • 19

  • 20

  • 21

  • 22

  • 23

  • 24

  • 25

  • 26

  • 27

  • 28

  • 29

輸出結(jié)果:

我是構(gòu)造函數(shù)。。。。成員函數(shù)類函數(shù)靜態(tài)函數(shù)普通方法

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多