簡體   English   中英

Python-如何在單獨的文件中創建類的實例

[英]Python - How to create an instance of a class in a separate file

我正在嘗試從名為Login.py的文件創建“播放器”類的新實例(在Player.py中)

    import Player
    pfile = "data/players/" + username
    s = shelve.open(pfile)
    s(username = Player.player(username, password, False))

從中我得到:

AttributeError: 'module' object has no attribute 'player'

我希望能夠創建對象並將其放置在字典中,該字典的鍵是播放器的用戶名,我在做什么錯呢? 謝謝

也許您的班級是Player,這意味着要引用它,您應該執行以下操作

import Player
my_player = Player.Player(username, password, False)

您還應該看看這條線

s(username = Player.player(username, password, False))

我不確定它是否有效,並且會像Martijn所指出的那樣工作,python會將用戶名視為關鍵字參數

暫無
暫無

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

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