How to fix this TypeError for missing 'self' argument

Getting TypeError on those methods where i used self as a param like missing 1 required positional argument: 'self'. If the method have more than 1 arguments including self it shows similar error of missing another positional argument.

I started by making a singleton object that checks its own references to remain its singularity. I used __new__ to do that.

core.py
import pyglet.window

class Core(object):

    def __new__(cls, *args, **kwargs):
        if not cls:
            cls = object.__new__(cls, *args, **kwargs)
        return cls

    def __init__(self):
        self._instance = None
        self._window = None

    def __repr__(self):
        return '<%s object at %s>' % (Core.__name__, hex(id(Core)))

    def scene(self, width, height):
        self._window = pyglet.window.Window(width=width, height=height)
script.py
from core import Core

c = Core()
print(c.__repr__())
c.scene(640, 480)
errors:
    print(c.__repr__())
TypeError: __repr__() missing 1 required positional argument: 'self'

    c.scene(640, 480)
TypeError: scene() missing 1 required positional argument: 'height'

This method is not the only case, so i want a understanding what is going on and how to fix this. Thank you for your time.

Edit:

It is not problem with only the__repr__. I want the other methods like scene(self, width, height) or any methods you could possibly create having self as one param. The second error for showing what error this code gives with other methods

I need the Core class to make a singleton object, so i can use other files to reference to this Core._window thing. I think this edited description can clarify what i wanted earlier. Sorry for convenience.


Oops, __new__ receives the class and is supposed to return the newly created object! As your code returns the class itself, thing go wrong. You should have:

class Core(object):

    def __new__(cls, *args, **kwargs):
        obj = object.__new__(cls, *args, **kwargs)
        return obj
    ...

And anyway __new__ is an advanced configuration, with is mainly used for immutable objects that cannot be configured in __init__. In your example you should just remove it:

class Core(object):

    def __init__(self):
        self._instance = None
        self._window = None

    def __repr__(self):
        return '<%s object at %s>' % (Core.__name__, hex(id(Core)))

That is enough for common objects. You can indeed use __new__ to build singletons, and even use it to build hierachies of singletons, meaning that every subclass of Core will be a singleton:

class Core(object):
    _obj = None
    def __new__(cls, *args, **kwargs):
        if cls._obj is None or not isinstance(cls._obj, cls):
            cls._obj = object.__new__(cls, *args, **kwargs)
        return cls._obj

    def __init__(self):
        self._instance = None
        self._window = None

    def __repr__(self):
        return '<%s object at %s>' % (Core.__name__, hex(id(self)))

Please note that my class __repr__ uses id(self) to identify the object and not the class.

Demo:

>>> c = Core()
>>> d = Core()
>>> c is d
True
>>> class Child(Core):
    pass

>>> ch = Child()
>>> ch is c
False
>>> ch2 = Child()
>>> ch2 is ch
True

How to fix `TypeError: get_sheet_by_name() missing 1 required positional argument: 'name' error` in `openpyxl` Ask Question Asked 1 year ago. Active 1 year ago.


The correct way is use repr like this:

repr(c)

1 Answer1. active oldest votes. 1. You need to call your ellipse function, and pass the returned values to the Ellipse () function from matplotlib. for ii in range(len(links[:, 0])): xy, width, height, angle = ellipse(a, b) ellipses.append(Ellipse(xy, width, height, angle))


Your code almost works. Take a look here:

class Core(object):

    _instance = None
    def __new__(cls, *args, **kwargs):
        if not cls._instance:
            cls._instance = object.__new__(cls, *args, **kwargs)
        return cls._instance

    def __init__(self):
        pass

    def __repr__(self):
        return '<%s object at %s>' % (Core.__name__, hex(id(self)))

core = Core()
print(id(core))

core1 = Core()
print(id(core1))

print(repr(core))
print(repr(core1))

The only way to permanently fix DLL errors is by fixing the underlying cause of the issue, not by downloading DLL files. Here's how to do it.


I'm learning the machine, I wrote the above code while trying to delete missing values TypeError: __init __ got an unexpected keyword argument I got a 'missing_vaules' error, where is the e


When you see ‘one or more network protocols are missing on this computer’ error, you can’t surf the internet or download files. Try these solutions!


To repair the “vcomp140.dll is missing” error, put the file inside the application/game installation folder. Alternatively, you can place the vcomp140.dll file within the Windows system directory.