文章来源:嗨学网 敏而好学论坛www.piaodoo.com 欢迎大家相互学习
1、概述
1.1 场景
我们在使用 Python 中的 方法 method 时,经常会看到 参数中带有 self,但是我们也没对这个参数进行赋值,那么这个参数到底是啥意思呢?
2、知识点
2.1 成员函数(m) 和 普通方法(f)
Python 中的 "类方法" 必须有一个额外的 第一个参数名称(名称任意,不过推荐 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.