2009-12-08 61 views
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() 

與該代碼我收到爲什麼我的Python類聲稱我有2個參數而不是1?

> 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) 

有什麼想法?

回答

16

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

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

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

您可能希望通過Python tutorial閱讀。

+1

謝謝大家,我忘了那件事。這是我用Python創建的第一個實際的類,所以感謝耐心。我開始使用Java,所以目前這感覺非常不同! – jphenow 2009-12-08 17:58:24

+0

是的,我記得當我第一次開始使用Python類時有類似的問題(更不用說有點惱火,我不得不在任何地方明確地寫「self」)。關於python最難的部分是學習忘記所有從Java或C++學到的東西:) – Seth 2009-12-08 19:19:39

1

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

0

在課堂上,您需要考慮方法成員的self參數。

2

你需要明確傳遞self變量,它代表一個類的實例,例如:

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

它沒有被稱爲self但它是遵循一個好習慣,你的代碼將被其他程序員理解。

+0

應該是** self **。user_read(),** self **。user_write()等。 – atzz 2009-12-08 17:57:05

+0

謝謝,atzz,當然這隻適用於同類方法 – SilentGhost 2009-12-08 18:00:31

0

由於您將set視爲類的綁定(實例)方法,因此您必須明確接收實例作爲第一個參數。按慣例稱之爲「自我」。

def set(self, file): 
    filepermission = os.stat(file) 
    user_read() 
    user_write() 
    user_exec() 
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)作爲第一個參數傳遞給方法

1

set()是類chkup的方法。當你打電話給i.set(place)時,python使用方法的第一個參數跟蹤實例i。通常,每個實例方法都會接收至少一個名爲self的參數,隨後的參數會隨之而來。你應該重新定義你的類:

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

你可能會查找「自我」和Python的計算器:

Python __init__ and self what do they do?

0

爲了定義一個非靜態方法你必須提供「自我」這樣的第一個參數

class chkup:

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

#這是爲了使非靜態方法,一套

#the()調用這裏由

CHK = chkup()

chk.set(文件名)#做筆記那你在調用時不提供「self」

0

那是因爲python會自動將當前對象作爲參數傳遞給類中的所有方法,所以當您將2個參數傳遞給一個函數時,python將追加第三個參數當前對象,方法親totype應該考慮這個

相關問題