How is following class initialization working | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
0

How is following class initialization working

class Mry: print " Hello Mry" After execution of above lines, it is printing o/p as Hello Mry My doubt here how is it printing w/o creating any object of class or calling class itself as in case of class attributes.

16th Oct 2019, 1:28 PM
Banana Face
Banana Face - avatar
1 Antwort
+ 2
It is printing because you have put ot directly inside the body of the class without defining an initiator. Try this: class Mry: def __init__(self): print("Hello Mry") This will print "Hello Mry" only when you construct it by: Mry() <code is for python 3>
21st Oct 2019, 6:22 AM
Jalaj Kumar
Jalaj Kumar - avatar