Knowee
Questions
Features
Study Tools

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")

🧐 Not the exact question you are looking for?Go ask a question

Solution

The output of the given Python code will be "Welcome".

Here's the step by step explanation:

  1. A class named myclass is defined.
  2. 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.
  3. The __init__ method takes two parameters - self and a. self is a reference to the current instance of the class and is used to access variables and methods that belongs to the class. a is a parameter that we will pass to the class when creating an object.
  4. Inside the __init__ method, a variable self.a is defined and assigned the value of a. Then, self.a is printed.
  5. An object o of the class myclass is created with the string "Welcome" passed as an argument. This will call the __init__ method, assign "Welcome" to self.a and print it.

This problem has been solved

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)

1/3

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.