博客
关于我
class类中def函数必须要(self)吗?
阅读量:365 次
发布时间:2019-03-04

本文共 283 字,大约阅读时间需要 1 分钟。

在编写Python类中的方法时,自定义参数`self`在许多资料中被强调为必须的。这种机制与解释器之间建立了一种默认的存储和读取机制。然而,通过具体实例可以看出,`self`并非在所有情况下都必须存在。

class Person:      def run():          print("没有self")  Person.run() # 直接运行run

运行代码可见,直接通过类属性调用函数时,并不需要设置`self`参数。然而,当使用实例化调用方法时,`self`参数是必不可少的。因为它用于将实例与方法参数建立联系,确保方法能够正确地处理对象属性和状态。

转载地址:http://atbr.baihongyu.com/

你可能感兴趣的文章
npm安装 出现 npm ERR! code ETIMEDOUT npm ERR! syscall connect npm ERR! errno ETIMEDOUT npm ERR! 解决方法
查看>>
npm安装crypto-js 如何安装crypto-js, python爬虫安装加解密插件 找不到模块crypto-js python报错解决丢失crypto-js模块
查看>>
npm安装教程
查看>>
npm报错Cannot find module ‘webpack‘ Require stack
查看>>
npm报错Failed at the node-sass@4.14.1 postinstall script
查看>>
npm报错File to import not found or unreadable: @/assets/styles/global.scss.
查看>>
npm报错unable to access ‘https://github.com/sohee-lee7/Squire.git/‘
查看>>
npm版本过高问题
查看>>
npm的“--force“和“--legacy-peer-deps“参数
查看>>
npm的安装和更新---npm工作笔记002
查看>>
npm的常用配置项---npm工作笔记004
查看>>
npm的问题:config global `--global`, `--local` are deprecated. Use `--location=global` instead 的解决办法
查看>>
npm编译报错You may need an additional loader to handle the result of these loaders
查看>>
npm设置淘宝镜像、升级等
查看>>
npm设置源地址,npm官方地址
查看>>
npm配置安装最新淘宝镜像,旧镜像会errror
查看>>
NPM酷库052:sax,按流解析XML
查看>>
npm错误 gyp错误 vs版本不对 msvs_version不兼容
查看>>
npm错误Error: Cannot find module ‘postcss-loader‘
查看>>
npm,yarn,cnpm 的区别
查看>>