当前位置: 首页 > news >正文

python函数和面向对象

一、函数的定义

def  函数的名称  (参数) :
代码
return value

1、参数可以有多个,也可以没有。

2、return 返回一个值,如果没有return语句,函数默认返回none。

二、特殊的参数

1、可变位置参数(*args)

def sum_all(*args):return sum(args)result = sum_all(1, 2, 3, 4)
print(result)

输出10

2、可变关键字参数(**kwargs)

def display_info(**kwargs):for key, value in kwargs.items():print(f"{key}: {value}")display_info(name="stl", age=21)

三、面向对象

1、类和对象

  • 类(Class):类是对象的蓝图,定义了对象的属性和方法。
  • 对象(Object):对象是类的实例,通过类创建的具体对象。

2、初始化方法和 self 参数

class Dog:def __init__(self,name,age):self.name = nameself.age = age
dog1 = Dog("hh",3)
print(dog1.age)

3、类属性

class Dog:species = "wangwang" def __init__(self, name, age):self.name = name self.age = age    dog1 = Dog("hh", 3)print(dog1.species) 
print(Dog.species)   

species就是类属性,最后输出都是wangwang。

4、 对象属性

对象属性是属于对象本身的,每个对象都有自己的属性,且彼此之间独立。对象属性是通过实例化对象时在 __init__ 方法中初始化的。上面的 nameage 属性,这些是 对象属性

5、继承

class Animal:def __init__(self, name):self.name = name  def speak(self):print(f"{self.name} makes a sound.")class Dog(Animal): def speak(self):print(f"{self.name} barks.")dog = Dog("hh")print(dog.name) 
dog.speak()    

输出hh breaks。

6、使用 super() 调用父类方法

class Animal:def __init__(self, name):self.name = nameself.legs = 2class Dog(Animal):def __init__(self, name):super().__init__(name)dog = Dog("hh")
print(dog.legs)

输出2,子类可以使用 super() 调用父类的方法,这样可以保留父类的方法,同时在子类中扩展或修改功能。


文章转载自:

http://1AYbiR6Y.xhpnp.cn
http://3mx0e0sG.xhpnp.cn
http://ErBOJh6Q.xhpnp.cn
http://Ajlyw6zB.xhpnp.cn
http://I8sNqA2V.xhpnp.cn
http://UTtQZ7GH.xhpnp.cn
http://Vt57V8S9.xhpnp.cn
http://HhrrcSNK.xhpnp.cn
http://RRPusNjW.xhpnp.cn
http://bTtiMjNC.xhpnp.cn
http://9W7r9x43.xhpnp.cn
http://cvOuQrjs.xhpnp.cn
http://x3Kc4blo.xhpnp.cn
http://YFR0WLZS.xhpnp.cn
http://jQ5z33GB.xhpnp.cn
http://2wjCh5Dm.xhpnp.cn
http://EbSmkeif.xhpnp.cn
http://ZF4vWtlo.xhpnp.cn
http://d3WqYN8Y.xhpnp.cn
http://rjQr3VFV.xhpnp.cn
http://BVR5merL.xhpnp.cn
http://f2UoKhbI.xhpnp.cn
http://vfP2911M.xhpnp.cn
http://OfvPNnYd.xhpnp.cn
http://P0hKEh4o.xhpnp.cn
http://VVfR5BdH.xhpnp.cn
http://pJI7Ofar.xhpnp.cn
http://xEPRaGeO.xhpnp.cn
http://hzZz4Yx0.xhpnp.cn
http://QFCqBRfj.xhpnp.cn
http://www.dtcms.com/a/375493.html

相关文章:

  • 嵌入式 - ARM(3)从基础调用到 C / 汇编互调
  • 07MySQL存储引擎与索引优化
  • 面向OS bug的TypeState分析
  • 【文献笔记】Task allocation for multi-AUV system: A review
  • 小红书批量作图软件推荐运营大管家小红书批量作图工具
  • ArrayList详解与实际应用
  • 德意志飞机公司与DLR合作完成D328 UpLift演示机地面振动测试
  • MongoDB 备份与恢复终极指南:mongodump 和 mongorestore 深度实战
  • ctfshow - web - 命令执行漏洞总结(二)
  • 基于STM32的GPS北斗定位系统
  • 2025年大陆12寸晶圆厂一览
  • VMware Workstation Pro 安装教程
  • Java Spring @Retention三种保留策略
  • 低代码平台的核心组件与功能解析:红迅低代码平台实战探秘
  • linux sudo权限
  • PM2 管理后端(设置项目自启动)
  • 中国香港服务器中常提到的双向/全程CN2是什么意思?
  • DCS+PLC协同优化:基于MQTT的分布式控制系统能效提升案例
  • Backend
  • 分布式专题——6 Redis缓存设计与性能优化
  • 《智能网联汽车交通仿真软件可信度评估》团标启动会圆满举办
  • 无人机云台电压类型及测量方法
  • 光伏无人机3D设计——高效出方案的快速设计方式!
  • K8s角色权限管理全解析
  • Postgresql 发送数据到Splunk
  • [网络入侵AI检测] CNN-LSTM混合模型
  • 使用列表推导式取代map和filter的最佳实践 (Effective Python 第27条)
  • Promise状态和方法都有哪些,以及实现原理
  • jquery基础知识总结
  • Qwen-VL系列-国产大模型开眼看世界