python使用类创建参数执行时为什么显示不接受参数

python使用类创建参数执行时为什么显示不接受参数,第1张

注意python类的构造函数是(开头结尾都是双下划线):

def __init__(self,kw):

    pass

而你写的_init_()函数少了下划线,也就不是Dog类的构造函数了,此时系统采用缺省的默认构造函数,即没有输入参数

是file类的构造函数,参数和内置的open()函数相同,在打开文件时更推荐使用open(),所以更多用于测试文件类型的测试:isinstance(f,file)

参考python275文档的解释:

file(name[, mode[,

buffering]])

Constructor function for the file type, described further in section File

Objects The constructor’s arguments are the same as those of the open()

built-in function described below

When opening a file, it’s preferable to use open()

instead of invoking this constructor directly file

is more suited to type testing (for example, writing isinstance(f, file))

python中file是一个变量,不是一个文件格式,Python中的是file类的构造函数,参数和内置的open()函数相同,在打开文件时更推荐使用open(),所以更多用于测试文件类型的测试:isinstance(f,file)

将open函数赋值给变量file只是为了使代码写起来更方便更美观

示例如下:

使用变量file之前:

1

2

open("axtxt", 'a')write('1234566')

open("axtxt", 'a')close()

使用变量file之后:

1

2

3

file = open("axtxt", 'a')

filewrite('1234566')

fileclose()

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/13494625.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2025-09-01
下一篇2025-09-01

发表评论

登录后才能评论

评论列表(0条)

    保存