簡體   English   中英

為什么我的Python類聲稱我有2個參數而不是1?

[英]Why does my Python class claim that I have 2 arguments instead of 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)

有什么想法嗎?

python類方法的第一個參數是self變量。 如果調用classInstance.method(parameter) ,則該方法將作為method(self, parameter)調用。

所以,當你定義你的課時,做這樣的事情:

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

您可能想要閱讀Python教程

因為您沒有將對象(通常稱為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 )作為第一個參數的方法

您需要顯式傳遞self變量,它表示類的實例,例如:

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

它不必被稱為self但它遵循一個很好的約定,其他程序員將理解您的代碼。

self是所有類成員函數的隱式第一個參數。 所以i.set(place)調用實際上調用了set(i, place) 在定義類時需要考慮這一點def set(self, file)而是編寫def set(self, file)

set()是類chkup的方法。 當你調用i.set(place) ,python使用方法的第一個參數跟蹤實例i。 通常,每個實例方法都將至少接收一個名為self的參數,然后是后續參數。 你應該重新定義你的課程:

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

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

Python __init__和self他們做了什么?

等等

在類中,您需要考慮方法成員的self參數。

由於您將set視為類的綁定(實例)方法,因此必須顯式接收實例作為第一個參數。 按照慣例,它被稱為“自我”。

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

為了定義一個非靜態方法,你必須提供“self”作為第一個參數

class chkup:

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

#這是為了制作非靜態方法,

#the set()的調用由此完成

CHK = chkup()

chk.set(fileName)#注意你在調用時不提供“self”

多數民眾贊成因為python自動將當前對象作為參數傳遞給類中的所有方法,所以當你向函數傳遞2個參數時,python會附加第三個參數,即當前對象,方法原型應該考慮這個

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM