为什么我的 Python 类声称我有 2 个参数而不是 1 个?
#! /usr/bin/env python
import os
import stat
import sys
class chkup:
def set(file):
filepermission = os.stat(file)
user_read()
user_write()
user_exec()
def user_read():
"""Return True if 'file' is readable by user
"""
# Extract the permissions bits from the file's (or
# directory's) stat info.
b = bool(filepermission.st_mode & stat.S_IRUSR)
print b
return b
def user_write():
"""Return True if 'file' is readable by user
"""
# Extract the permissions bits from the file's (or
# directory's) stat info.
b = bool(filepermission.st_mode & stat.S_WRUSR)
print b
return b
def user_exec():
"""Return True if 'file' is readable by user
"""
# Extract the permissions bits from the file's (or
# directory's) stat info.
b = bool(filepermission.st_mode & stat.S_IXUSR)
print b
return b
def main():
i = chkup()
place = '/net/home/f08/itsrsw1/ScriptingWork/quotacheck'
i.set(place)
if __name__ == '__main__':
main()
通过该代码我收到
> Traceback (most recent call last):
File "chkup.py", line 46, in <module>
main()
File "chkup.py", line 43, in main
i.set(place)
TypeError: set() takes exactly 1 argument (2 given)
任何想法?
#! /usr/bin/env python
import os
import stat
import sys
class chkup:
def set(file):
filepermission = os.stat(file)
user_read()
user_write()
user_exec()
def user_read():
"""Return True if 'file' is readable by user
"""
# Extract the permissions bits from the file's (or
# directory's) stat info.
b = bool(filepermission.st_mode & stat.S_IRUSR)
print b
return b
def user_write():
"""Return True if 'file' is readable by user
"""
# Extract the permissions bits from the file's (or
# directory's) stat info.
b = bool(filepermission.st_mode & stat.S_WRUSR)
print b
return b
def user_exec():
"""Return True if 'file' is readable by user
"""
# Extract the permissions bits from the file's (or
# directory's) stat info.
b = bool(filepermission.st_mode & stat.S_IXUSR)
print b
return b
def main():
i = chkup()
place = '/net/home/f08/itsrsw1/ScriptingWork/quotacheck'
i.set(place)
if __name__ == '__main__':
main()
With that code I receive
> Traceback (most recent call last):
File "chkup.py", line 46, in <module>
main()
File "chkup.py", line 43, in main
i.set(place)
TypeError: set() takes exactly 1 argument (2 given)
Any thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
Python 类方法的第一个参数是 self 变量。如果调用
classInstance.method(parameter)
,该方法将作为method(self,parameter)
调用。因此,当您定义类时,请执行以下操作:
您可能需要通读 Python 教程。
The first argument for a python class method is the
self
variable. If you callclassInstance.method(parameter)
, the method is invoked asmethod(self, parameter)
.So, when you're defining your class, do something like this:
You might want to read through the Python tutorial.
因为您没有将对象(通常称为
self
)作为第一个参数传递给您的方法。在 Python 中,像这样的调用:本质上被脱糖为这样的调用:
因此,Python 正在寻找像这样的方法签名:
所以你应该将对象(通常称为
self
)作为第一个传递方法的参数。Because you're not passing the object (generally referred to as
self
) as the first parameter to your methods. In Python, a call like this:is essentially desugared into a call like this:
Thus, Python is looking for a method signature like this:
So you should pass the object (generally called
self
) as the first parameter to a method.您需要显式传递
self
变量,它代表类的实例,例如:它不必称为
self
但这是一个很好遵循的约定,并且你的代码将被其他程序员理解。You need to explicitly pass
self
variable, which represents an instance of a class, e.g.:It doesn't have to be called
self
but it's a good convention to follow, and your code will be understood by other programmers.self 是所有类成员函数的隐式第一个参数。因此,
i.set(place)
调用实际上调用了set(i, place)
。您在定义类时需要考虑到这一点,并改为编写def set(self, file)
。self
is an implicit first argument to all class member functions. So thei.set(place)
call actually callsset(i, place)
. You need to take this into account when defining your class, and writedef set(self, file)
instead.set()
是chkup
类的方法。当您调用 i.set(place) 时,Python 使用该方法的第一个参数来跟踪实例 i。一般来说,每个实例方法都会接收至少一个称为 self 的参数,后面跟着后续的参数。你应该重新定义你的类:你可以在stackoverflow上查找“self”和python:
Python __init__ 和 self 他们做什么?
等等。
set()
is a method of classchkup
. When you calli.set(place)
, python keeps track of the instance i using the first argument to the method. Generally, every instance method will receive at least one argument, called self, and subsequent arguments follow. You should redefine your class:You might look up "self" and python on stackoverflow:
Python __init__ and self what do they do?
etc.
在类中,您需要考虑方法成员的
self
参数。In a class, you need to take into account the
self
parameter for method members.由于您将
set
视为类的绑定(实例)方法,因此您必须显式接收实例作为第一个参数。按照惯例,它被称为“自我”。Since you're treating
set
as a bound (instance) method of a class, you must explicitly receive the instance as your first argument. It's called "self" by convention.为了定义一个非静态方法,你必须提供“self”作为第一个参数,就像这个
类 chkup:
#this is do to make non staticmethod,
#the call of set() here did by
chk=chkup()
chk .set(fileName) # 请注意,调用时不要提供“self”
in order to define a non-static method you must provide "self" as a first argument like this
class chkup:
#this is done to make non static methods,
#the call of set() here done by
chk=chkup()
chk.set(fileName) # note that you dont provide "self" when calling
那是因为python自动将当前对象作为参数传递给类中的所有方法,所以当你向函数传递2个参数时,python附加第三个参数,即当前对象,方法原型应该考虑这一点
Thats because python automatically passes the current object as an argument to all the methods in the class,so when you pass 2 arguments to a function,python appends the third argument which is the current object,the method prototype should consider this