[Python] Why can a python class using pygame's Vector2 as a parent not have an attribute name...

Discussão em 'Python' iniciado por Stack, Fevereiro 10, 2025.

  1. Stack

    Stack Membro Participativo

    This is really weird, but I'm making a custom vector using pygame's Vector2 as a parent, and any attempt to define an attribute that starts with the letters x or y results in an error upon initialization. If the x is anywhere else in the attribute name, it works fine.

    For example, this works fine:

    class CustomVector(pygame.Vector2):
    def __init__(self,vector,attribute):
    super().__init__(vector)
    self.attribute = attribute

    CustomVector((0,0),True)


    This also works fine:

    class CustomVector(pygame.Vector2):
    def __init__(self,vector,attribute):
    super().__init__(vector)
    self.attributex = attribute

    CustomVector((0,0),True)


    But this raises an error:

    class CustomVector(pygame.Vector2):
    def __init__(self,vector,attribute):
    super().__init__(vector)
    self.xattribute = attribute

    CustomVector((0,0),True)


    The error raised is generally TypeError: a sequence is expected, but I've gotten others

    Per John Gordon, here is the whole error message. There is another error, but it is clearly stated that it is the direct result of the TypeError:

    TypeError: a sequence is expected

    The above exception was the direct cause of the following exception:


    Traceback (most recent call last):


    File "c:\Users\ajhom\Documents\Monster\code\test.py", line 14, in <module>
    CustomVector((0,0),True)
    ~~~~~~~~~~~~^^^^^^^^^^^^
    SystemError: <function CustomVector.__init__ at 0x000002B7C347EDE0> returned a result with an exception set

    Continue reading...

Compartilhe esta Página