pythonursina

Function() argument 'code' must be code, not str


I am trying to use the ursina module, and just entered this code:

from ursina import *

class Game(Ursina):
    def __init__(self):
        pass

It generates this error:

>>> from ursina import *
>>> class Game(Ursina):
>>> ...     def __init__(self):
>>> ...
Traceback (most recent call last):
File "<test>", line 3, in <module>
class Game(Ursina):
TypeError: function() argument 'code' must be code, not str

I have no code argument, so I have no clue what this is referring to.

Can someone explain and fix this error?


Solution

  • See Ursina issue 579.

    This is a limitation of the Ursina package which doesn't let you subclass the Ursina class.

    The Ursina class is defined as:

    @singleton
    class Ursina(ShowBase):
        ...
    

    And singleton is:

    def singleton(cls, **kwargs):
        def getinstance(**kwargs):
            if not hasattr(cls, '_instance') or not cls._instance:
                cls._instance = cls(**kwargs)
            return cls._instance
    
        return getinstance
    

    This means that the decorated Ursina is actually the function returned by singleton rather than a class, and so it can't be subclassed.