Python quiz help - class without instantiating? | Sololearn: Learn to code for FREE!
¡Nuevo curso! ¡Todo programador debería aprender IA Generativa!
Prueba una lección gratuita
0

Python quiz help - class without instantiating?

class System: def __init__(self, x): self.x = x class out: def println(other): print(other) try: System.out.println("ok") except: print("no") This was one of the quiz in the challenge. The answer is “ok”. I’m not understanding how System.out.println can take parameter in the println method without an instance of a class or subclass.

14th Nov 2021, 11:48 PM
Han Lee
Han Lee - avatar
1 Respuesta
+ 7
println() here is an undecorated static method that belongs to the inner class out. This means the only way this method can be called is via the class name and not through an instance. Unlike a static method that has the @staticmethod decorator, which can be called either via the class name or through an instance of the class. You can recognize a static method by the parameters not having the 1st parameter as self or cls. A method that starts with self as the 1st parameter is an instance method, while a method that has the 1st parameter as cls is a class method and should also have the @classmethod decorator.
15th Nov 2021, 12:28 AM
ChaoticDawg
ChaoticDawg - avatar