简体   繁体   English

Python-如何在单独的文件中创建类的实例

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

I'm trying to create a new instance of the class "player" (in Player.py) from a file called Login.py 我正在尝试从名为Login.py的文件创建“播放器”类的新实例(在Player.py中)

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

And from that I get: 从中我得到:

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

I want to be able to create the object and place it in a dictionary where its key is the username of the player, what am I doing wrong? 我希望能够创建对象并将其放置在字典中,该字典的键是播放器的用户名,我在做什么错呢? Thanks 谢谢

Perhaps your class is Player this would mean that to reference it you should do something like this 也许您的班级是Player,这意味着要引用它,您应该执行以下操作

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

You should also look at this line 您还应该看看这条线

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

Im not sure its valid and will work as Martijn pointed out, python will treat username as a keyword argument 我不确定它是否有效,并且会像Martijn所指出的那样工作,python会将用户名视为关键字参数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM