Junior — Middle
Is it possible to implement a function that works without creating an instance of a class?
sobes.tech AI
Answer from AI
Yes, in Python, you can implement a function that works without creating an instance of a class by using the @staticmethod decorator.
Example:
class MyClass:
@staticmethod
def greet(name):
return f"Hello, {name}!"
# Call without creating an instance
print(MyClass.greet("World")) # Outputs: Hello, World!
Static methods do not take the self parameter and can be called directly through the class.