Search code examples
pythonpygametuplesnonetype

Initialized tuple element becoming NoneType


I init a tuple here

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

and whenever I try to access any of the player's classes members from that element I get a NoneType object has no attribute 'model' error:

players[0].model.pos = mousepos

I don't understand why I can't use that element because I am initializing that element as type 'Player'

Thanks in advance

Edit: keep in mind that I make no modifications to players or any of it's elements in between those 2 snippets

Edit2: Class definitions

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

Solution

  • You should use __init__ instead of __new__

    Here is an example for __init__ and __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()
    

    Code achieved from https://www.jianshu.com/p/14b8ebf93b73 (Chinese website)