![](/img/trans.png)
[英]How to call a method of an object returned from an async function without parentheses in Python?
[英]Is there a way to call a method on a Python object returned by a function?
所以我正在 Python 中構建一個命令行教程程序。 基本上它模擬一個終端並使用 shell 命令引導您完成不同的級別。 原始版本的工作方式是我捕獲輸入的任何內容並分析它以調用教程程序(如果沒有任何內容)我使用 os.system() 將命令傳遞給計算機。
不幸的是,由於我編寫的教程是針對 *nix 系統的,因此它不適用於 Windows,因為我包含了使用 vim 之類的關卡。 我的計划是重寫程序,但不是調用系統,而是創建一個虛擬文件系統。 這包括對象:目錄和文件。 您可能會弄清楚這兩個對象的含義。 目錄 object 將包含其內容列表,這將充當文件系統。 然后,我將執行 cd、ls、pwd 等命令。
我遇到的問題是,當我從目錄 object 返回包含目錄或文件時,我無法以任何方式對其進行編輯。 我會給你一個我的代碼的例子。
注意:代碼中可能存在錯誤,因為如果不解決此問題,我無法很好地測試它此外,導入對於問題來說不是必需的。 第一個是顏色 class ,它列出了執行彩色 output 的代碼,第二個是包含全局變量的文件。 我已經刪除了對這些資源的調用,以便它可以在其他人的系統上運行
#from ..resources.colors import colors
#from ..resources import variables as var
class Directory:
# Basic constructor
def __init__(self, name="", container=None, contents=[]):
self.name = name
self.container = container
self.contents = contents
# grabs index of contents by name
def index(self, find):
for i in range(len(self.contents)):
if self.contents[i].name == find:
return i
return -1
# Add something to the container
def add(self, dirFile):
# Check for duplicate names
if self.index(dirFile.name) != -1:
return False
self.contents.append(dirFile)
return True
# Retrieve something that's in contents
def get(self, neededName):
ind = self.index(neededName)
if ind >= 0:
return self.contents[ind]
# Returns true if it has at least one sub directory
def has_sub(self):
for content in self.contents:
if type(content) == Directory:
return True
return False
# Iterates through, looks for slashes and goes to subs (recursion, ooooh!)
def get_sub(self, path=""):
if not self.has_sub() or path == "":
return self
pathSplit = path.split("/")
nextDir = self.index(pathSplit[0])
if nextDir >= 0:
return self.contents[nextDir]
pathSplit.pop(0)
path = "/".join(pathSplit)
return self.get_sub(path)
# ============== Shell Commands ==============
# List all contents in directory
def ls(self):
for content in self.contents:
if type(content) == Directory:
#print(colors.fg.blue + content.name + colors.reset + "\t", end="") This is the code wih the colors class
print(content.name + "\t", end="")
else:
print(content.name + "\t", end="")
print()
# Delete file by name:
def rm(self, toDelete):
ind = self.index(toDelete)
if ind >= 0:
self.contents.pop(ind)
return True
else:
return False
# Make new dir
def mkdir(self, name=""):
if name:
self.add(Directory(name))
## Current test
di = Directory("Test")
di.mkdir("one")
di.mkdir("two")
di.get_sub("one").mkdir("test")
di.ls()
di.get_sub("one").ls()
當前的output為:
one two test
one two test
但是 output 應該是:
one two
test
出於某種原因,當我打電話時:
di.get_sub("one").mkdir("test")
它改變了 di 而不是 get_sub() 返回的 object
任何幫助都非常感謝。 讓我知道是否還有其他我應該提供的東西。
我想我可以給你一些建議。 第一個可能最合適的是回答您的主要問題:
它改變了 di 而不是 get_sub() 返回的 object
這很可能是因為無論事情如何發展,has_sub() 最終都會解析到一個沒有其他目錄的子目錄。這將導致該子句變為 true:
if not self.has_sub() or path == "":
return self
if not self.has_sub
將評估 true 並返回di
。 它總是di
因為你的遞歸例程總是調用self
上的方法,即di
。 我希望這是有道理的!
關於我可以給你的第二個指針,也許我可以問幾個關於你的測試用例的問題,這可能有助於一些發現和通過過程:
1: di = Directory("Test")
2: di.mkdir("one")
3: di.mkdir("two")
4: di.get_sub("one").mkdir("test")
5: di.ls()
6: di.get_sub("one").ls()
在第一行創建Test
,然后添加 (2) 和 (3) 新的(我想)子目錄。 但是,在您的 Directory 實現中查看 mkdir ,這些實際上並沒有被創建。 也許看看os.mkdir並考慮您是否需要擔心現有目錄。 os.path.join也可能是你的朋友......
好好看看你要返回的東西。 如果您return self
您將返回對您調用該方法的 object 的引用。 如果你將第二個方法調用鏈接到那個(在你的情況下)它仍然是你調用它的di
。
我認為,如果您首先開始研究os.path作為一個模塊,然后考慮一些附加的 os 函數,例如上面提到的 mkdir,那么您將獲得很多樂趣。 還要考慮到os.getcwd()和os.chdir()可能很有趣。
我真的希望這會有所幫助。 :-)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.