日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

Python self 參數(shù)詳解

 Four兄 2019-08-31

文章目錄

1、概述

1.1 場景

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

2、知識點

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

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

  • m、f、c 都是代碼自動提示時的 左邊字母(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ù)普通方法

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

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多