what is the output of following python code? class myclass: def __init__(self,a): self.a = a print(self.a)o=myclass("Welcome")
Question
what is the output of following python code? class myclass: def init(self,a): self.a = a print(self.a)o=myclass("Welcome")
Solution
The output of the given Python code will be "Welcome".
Here's the step by step explanation:
- A class named
myclassis defined. - Inside this class, a special method called
__init__is defined. This is a constructor method that is automatically called when an object of this class is created. - The
__init__method takes two parameters -selfanda.selfis a reference to the current instance of the class and is used to access variables and methods that belongs to the class.ais a parameter that we will pass to the class when creating an object. - Inside the
__init__method, a variableself.ais defined and assigned the value ofa. Then,self.ais printed. - An object
oof the classmyclassis created with the string "Welcome" passed as an argument. This will call the__init__method, assign "Welcome" toself.aand print it.
Similar Questions
what is the output of following python code? class myclass: def display(self,a): print(a)o=myclass()o.display("Hello World")
What will be the output of the following code?class MyClass: def __init__(self, value): self.value = valueobj = MyClass(10)print(obj.value)
What do these lines print?class Base(): """ My base class """ __nb_instances = 0 def __init__(self): Base.__nb_instances += 1 self.id = Base.__nb_instancesclass User(Base): """ My User class """ def __init__(self): self.id = 89u = User()print(u.id)
What will be the output of the following Python code?class program: def __init__(self, param): self.o1 = param class code(program): def __init__(self, param): self.o2 = param ct = code(22)print ("%d %d" % (ct.o1, ct.o2))OptionsNone NoneNone 2222 NoneError is generated
What will be the output of below Python code?class A(): def __init__(self,count=100): self.count=countobj1=A()obj2=A(102)print(obj1.count)print(obj2.count)
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.