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

有什么想法吗?

4

9 回答 9

16

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

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

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

您可能想通读Python 教程

于 2009-12-08T17:54:37.247 回答
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)作为第一个参数传递给方法

于 2009-12-08T17:55:48.587 回答
2

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

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

它不必被调用self,但它是一个很好的约定,并且您的代码将被其他程序员理解。

于 2009-12-08T17:54:49.383 回答
1

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

于 2009-12-08T17:54:15.703 回答
1

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

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

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

Python __init__ 和 self 他们做了什么?

等等

于 2009-12-08T18:00:18.127 回答
0

在一个类中,您需要考虑self方法成员的参数。

于 2009-12-08T17:54:30.650 回答
0

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

def set(self, file):
    filepermission = os.stat(file)
    user_read()
    user_write()
    user_exec()
于 2009-12-08T17:55:43.737 回答
0

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

chkup 类:

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

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

#set() 的调用在这里完成

chk=chkup()

chk.set(fileName) # 注意调用时不提供“self”

于 2009-12-10T08:51:13.687 回答
0

那是因为python自动将当前对象作为参数传递给类中的所有方法,所以当你向函数传递2个参数时,python附加第三个参数是当前对象,方法原型应该考虑这一点

于 2010-01-02T18:25:16.297 回答