初始化的元组元素变为NoneType

时间:2019-04-26 15:44:32

标签: python pygame tuples nonetype

我在这里创建一个元组

players = [Player("Local", (0,0), 20)]

并且每当我尝试从该元素访问玩家的任何类成员时,我都会得到一个NoneType对象,该对象没有属性'model'错误:

players[0].model.pos = mousepos

我不明白为什么不能使用该元素,因为我正在将该元素初始化为'Player'类型

预先感谢

编辑:请记住,在这两个摘要之间,我没有对播放器或其中的任何元素进行任何修改

Edit2:类定义

class Circle():
    pos = (0,0)
    radius = 0.0

class Player():
    name = ""
    model = Circle()
    def __new__(self, Name = "", pos = (0,0), radius = 0):
        name = Name
        model = Circle()
        model.pos = pos
        model.radius = radius

1 个答案:

答案 0 :(得分:1)

您应该使用__init__而不是__new__

以下是__init____new__的示例:

class newStyleClass(object): 
    # In Python2, we need to specify the object as the base.
    # In Python3 it's default.

    def __new__(cls):
        print("__new__ is called")
        return super(newStyleClass, cls).__new__(cls)

    def __init__(self):
        print("__init__ is called")
        print("self is: ", self)

newStyleClass()

https://www.jianshu.com/p/14b8ebf93b73获得的代码 (中文网站)