首页 > 编程语言 >Python 类内、类间 函数的调用

Python 类内、类间 函数的调用

时间:2023-02-27 22:14:50浏览次数:36  
标签:__ return triple 类间 Python self 类内 class def

类内函数的调用

class A():

	def double(self,x):
		return x*2

	def triple(self,y):
		return y*3

	def sum(self,x,y):
		return self.double(x)+self.triple(y)

if __name__ == '__main__':
	print(A().sum(5,6))

结果为:28

类间函数的调用

class A():

	def double(self,x):
		return x*2

	def triple(self,y):
		return y*3
class B(A):
	def sum(self,x,y):
		return super().double(x)+super().triple(y)

if __name__ == '__main__':
	print(B().sum(5,6))

定义类 class A:class A():class A(object)这三种没有区别。
可以通过dir()查询属性

['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'double', 'sum', 'triple']

标签:__,return,triple,类间,Python,self,类内,class,def
From: https://www.cnblogs.com/conpi/p/17162144.html

相关文章

  • python / lib
    spython正则表达式:repython操作系统库:ospython爬虫库:request库C:\Users\lindows>pipinstallrequests-ihttps://mirrors.aliyun.com/pypi/simpleLookinginindex......
  • Python中logging模块用法
    一、低配logging日志总共分为以下五个级别,这个五个级别自下而上进行匹配debug-->info-->warning-->error-->critical,默认最低级别为warning级别。1.v1importlogging......
  • python爬虫-request模块
    1.requests中的请求方法  HTTP请求方法:requests.get(url,params=None,**kwargs)#GET请求requests.post(url,data=None,json=None,**kwa......
  • 如何选择Python与C++之间的胶水:Boost.Python,Cython,pybind11,SWIG
    Python作为一门胶水语言,它与C/C++之间的兼容性(Interoperability)我认为是它相比其他动态语言脱颖而出的最大原因。Python原生支持的是与C语言的接口,Python的发行版自带有Pyt......
  • ChatGPT: python3 查找在列表1中而不再列表2的项目
    A:f1_urls=['a','b']f2_urls=['a','c']python3中获取存在于f1_urls而不在f2_urls的项目Q:f1_urls=['a','b']f2_urls=['a','c']#将列表转换为集......
  • python用turtle画出给定图片的图像
    python用turtle画出给定图片的图像、校徽等复杂图像都可以需要:1.要画的图片2.安装好cv和turtle打开python文件,把想画的图片放到和py文件同目录,代码中默认图片名字为1.xxxxx......
  • python算法基础
    一、简介定义和特征定义:算法(Algorithm)是指解题方案的准确而完整的描述,是一系列解决问题的清晰指令,算法代表着用系统的方法描述解决问题的策略机制。也就是说,能够对一定......
  • python通过snmp获取交换机端口状态
    frompysnmp.entity.rfc3413.onelinerimportcmdgenclassNumberStatus:def__int__(self,host):self.host=hostdefwalk(self,dswitch,comm......
  • python -- PyQt5(designer)安装详细教程
    摘自:https://blog.csdn.net/weixin_64338372/article/details/128111818先展示一下安装好后的效果如下:  PyQt5基本教程大全​​​​​​​http://t.csdn.cn/L50jl......
  • 【Python】判断字符串输入合法化
    Python判断字符串输入合法化​​只包含数字​​​​包含数字​​​​只包含中文​​​​包含中文​​​​只包含字母​​​​包含字母​​只包含数字判断字符串是否只包含数......