Function in a class doesn't require self parameter

Question:

Trying to make a Python module which provides functions for user input.
My code looks like this:

class PyInput:
    def inputString(prompt = "Enter a string: >>> "):
        userInput = input(prompt)
        return userInput

I’m receiving the following error when I run the function:

Instance methods should take a "self" parameter

I have tried to add a self parameter to the function, but this requires the user to make an object of the class first, which isn’t what I want.
Is there a way to define a function within a class that so it doesn’t take a self parameter?

I’m running Python 3.11 on a Windows 11 64-bit laptop.

Asked By: Ben the Coder

||

Answers:

Actually, there is a way to define functions inside a class without the self parameter if you decorate it using @staticmethod. For example:

class Example:

    @staticmethod
    def answer():
        print('42')
Answered By: Suprateem Banerjee
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.