域名查询网站入口,学做网站论坛课程,北京最大做网站的公司,如何开发手机端网站Python中的函数和方法概要 1. 函数#xff08;Function#xff09;
定义#xff1a;独立存在#xff0c;不依赖类或对象。
调用方式#xff1a;直接通过函数名调用。
参数传递#xff1a;显式传递所有参数。
示例
# 独立函数
def add(a, b):return a bprint(add(3,…Python中的函数和方法概要 1. 函数Function
定义独立存在不依赖类或对象。
调用方式直接通过函数名调用。
参数传递显式传递所有参数。
示例
# 独立函数
def add(a, b):return a bprint(add(3, 5)) # 输出82. 方法Method
定义定义在类内部方法是定义在类内部的函数分为三种类型
☆实例方法操作对象实例绑定到对象。
☆类方法用于操作类状态类属性或提供替代构造函数绑定到类。定义时需使用classmethod装饰器第一个参数约定为cls代表类本身的引用。可通过类名或实例名调用。
☆静态方法定义在类内部但既不绑定到对象实例也不绑定到类定义时需使用staticmethod装饰器无隐式参数。它逻辑上不依赖特定实例或类状态通常用于组织与类相关的工具函数但语法上属于类命名空间。可通过类名或实例名调用后者不推荐。 参数传递
☆实例方法隐式传递self对象实例。
☆类方法隐式传递cls类本身。
☆静态方法无隐式参数。 示例
☆Python实例方法
class Animal:def __init__(self, name):# 初始化实例属性self.name namedef make_sound(self):实例方法返回动物的叫声依赖对象实例if self.name Dog:return Woof!elif self.name Cat:return Meow!return Unknown sounddef show_info(self):实例方法显示动物信息return fAnimal: {self.name}, Sound: {self.make_sound()}# 创建实例
dog Animal(Dog)
cat Animal(Cat)
unknown_animal Animal(Bird)# 调用实例方法通过对象名.方法名()
print(dog.make_sound()) # 输出Woof!
print(cat.show_info()) # 输出Animal: Cat, Sound: Meow!
print(unknown_animal.show_info()) # 输出Animal: Bird, Sound: Unknown soundself 类似 Java 的 this隐式传递对象实例。 ☆Python类方法
class DateProcessor:# 类属性存储日期格式date_format %Y-%m-%dclassmethoddef format_date(cls, year, month, day):类方法将年、月、日格式化为指定字符串cls指向DateProcessor类本身return f{year}-{month:02d}-{day:02d}classmethoddef parse_date(cls, date_str):类方法解析日期字符串为年、月、日元组year, month, day map(int, date_str.split(-))return (year, month, day)# 调用类方法通过类名直接调用
formatted DateProcessor.format_date(2025, 6, 12)
print(formatted) # 输出2025-06-12parsed DateProcessor.parse_date(2025-06-12)
print(parsed) # 输出(2025, 6, 12)# 类方法也可通过实例调用但推荐用类名调用
processor DateProcessor()
print(processor.format_date(2025, 6, 13)) # 输出2025-06-13
通过classmethod定义隐式传递cls参数。 ☆Python静态方法
class MathUtils:数学工具类staticmethoddef is_prime(n):判断一个数是否为质数静态方法if n 1:return Falsefor i in range(2, int(n**0.5) 1):if n % i 0:return Falsereturn Truestaticmethoddef circle_area(radius):计算圆的面积静态方法return 3.14159 * radius ** 2# 通过类名直接调用推荐方式
print(MathUtils.is_prime(17)) # 输出: True
print(MathUtils.is_prime(15)) # 输出: False
print(MathUtils.circle_area(3)) # 输出: 28.27431# 通过实例调用可行但不推荐
utils MathUtils()
print(utils.circle_area(5)) # 输出: 78.53975
通过staticmethod定义不绑定到实例或类即不会自动传递 self 或 cls但它仍然定义在类的命名空间内。
区别小结 特点 函数 实例方法 类方法 静态方法 归属 独立 绑定到对象 绑定到类 属于类不绑定 隐式参数 无 self - 对象实例 cls - 类引用 无 调用方式 函数名 对象名.方法名() 类名.方法名() / 对象名.方法名() 类名.方法名() / 对象名.方法名() (后者不推荐) 其它 需classmethod 需staticmethod
其中self和cls是强大的约定PEP 8 推荐self代表对象实例cls代表类本身的引用”。classmethod 和 staticmethod 是装饰器必须使用这些装饰器才能正确地定义类方法和静态方法。 附录
Python青少年简明教程函数 https://blog.csdn.net/cnds123/article/details/141500961
Python青少年简明教程类和对象入门 https://blog.csdn.net/cnds123/article/details/141953553
Python中的self参数介绍 https://blog.csdn.net/cnds123/article/details/148336578
Python中的变量、赋值及函数的参数传递概要https://blog.csdn.net/cnds123/article/details/148203255
python装饰器(Decorator)再谈 https://blog.csdn.net/cnds123/article/details/131234738
Python类的成员介绍 https://blog.csdn.net/cnds123/article/details/130898914
Python面向对象程序设计讲座 https://blog.csdn.net/cnds123/article/details/108354860
Python命名空间Namespaces和作用域Scopes讲座 https://blog.csdn.net/cnds123/article/details/108429084
Python嵌套函数Nested function和闭包closurehttps://blog.csdn.net/cnds123/article/details/129666657