为什么我的Python类声称我有2个参数而不是1?

时间:2009-12-08 17:52:50

标签: python class-method

#! /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)

有什么想法吗?

9 个答案:

答案 0 :(得分:16)

python类方法的第一个参数是self变量。如果您致电classInstance.method(parameter),则该方法会被调用为method(self, parameter)

因此,当您定义课程时,请执行以下操作:

class MyClass(Object): 
    def my_method(self, parameter): 
        print parameter

您可能需要阅读Python tutorial

答案 1 :(得分:4)

因为您没有将对象(通常称为self)作为方法的第一个参数传递。在Python中,这样的调用:

my_obj.do_something(my_other_obj)

基本上是这样的一个电话:

MyClass.do_something(my_obj, my_other_obj)

因此,Python正在寻找这样的方法签名:

class MyClass(object):
    def do_something(self, my_other_obj):
        self.my_var = my_other_obj

因此,您应该将对象(通常称为self)作为第一个参数传递给方法

答案 2 :(得分:2)

您需要显式传递self变量,它代表一个类的实例,例如:

def set(self, file):
    filepermission = os.stat(file)
    self.user_read()
    self.user_write()
    self.user_exec()

它不必被称为self,但它遵循一个很好的约定,其他程序员将理解您的代码。

答案 3 :(得分:1)

self是所有类成员函数的隐式第一个参数。因此i.set(place)来电实际上会调用set(i, place)。您需要在定义课程时考虑到这一点,并改为编写def set(self, file)

答案 4 :(得分:1)

set()是类chkup的方法。当你调用i.set(place)时,python使用方法的第一个参数跟踪实例i。通常,每个实例方法都将至少接收一个名为self的参数,然后是后续参数。你应该重新定义你的班级:

class chkup:
    def set(self, file):
        "etc..."

您可以在stackoverflow上查找“self”和python:

Python __init__ and self what do they do?

等。

答案 5 :(得分:0)

在课程中,您需要考虑方法成员的self参数。

答案 6 :(得分:0)

由于您将set视为类的绑定(实例)方法,因此必须显式接收该实例作为第一个参数。按惯例,它被称为“自我”。

def set(self, file):
    filepermission = os.stat(file)
    user_read()
    user_write()
    user_exec()

答案 7 :(得分:0)

为了定义一个非静态方法,你必须提供“self”作为第一个参数,如

class chkup:

    def set(self,file):
            filepermission = os.stat(file)

#这是为了制作非静态方法,

#the调用set()来完成

CHK = chkup()

chk.set(fileName)#注意你在调用

时不提供“self”

答案 8 :(得分:0)

多数民众赞成因为python会自动将当前对象作为参数传递给类中的所有方法,所以当你将2个参数传递给函数时,python会附加第三个参数,即当前对象,方法原型应该考虑这个< / p>