class Points(object): def __init__(self,x,y): self.x=x self.y=y def print_point(self): print('x=',self.x,' y=',self.y)p1=Points("A","B")p1.print_point()
Question
class Points(object): def init(self,x,y): self.x=x self.y=y def print_point(self): print('x=',self.x,' y=',self.y)p1=Points("A","B")p1.print_point()
Solution
The code you provided is written in Python. It defines a class named Points with two methods: __init__ and print_point.
Here's a step-by-step explanation:
-
class Points(object):This line is defining a new class namedPoints. The(object)part means thatPointsis a subclass ofobject. -
def __init__(self,x,y):This line defines the initializer method for the class. This method is automatically called when you create a new instance of the class. Theselfparameter is a reference to the current instance of the class, andxandyare parameters that will be passed to the method when it's called. -
self.x=xandself.y=yThese lines are setting thexandyattributes of the current instance of the class. -
def print_point(self):This line defines a new method namedprint_pointthat takes one parameter:self. -
print('x=',self.x,' y=',self.y)This line is inside theprint_pointmethod, and it prints thexandyattributes of the current instance of the class. -
p1=Points("A","B")This line is creating a new instance of thePointsclass, passing"A"and"B"as thexandyparameters, and assigning the new instance to the variablep1. -
p1.print_point()This line is calling theprint_pointmethod on thep1instance of thePointsclass. This will print thexandyattributes of thep1instance, which are"A"and"B", respectively.
Similar Questions
If there is a class Point of two attributes x and y. what is output of the following code? from copy import copyQ = Point(3,4)P = copy(Q)P.x = 5print(Q.x)
class Demo: def __init__(self): self.a = 1 self.__b = 1 def get(self): return self.__bobj = Demo()obj.a=45print(obj.a)
What will be the output of the following code?class MyClass: def __init__(self, value=5): self.value = valueobj1 = MyClass()obj2 = MyClass(10)print(obj1.value, obj2.value)
class One: def __str__(self): return '1'class Two(One): def __init__(self): super().__init__()class Three(Two): def __init__(self): super().__init__()def main(): object1 = Two() object2 = One() object3 = Three() print(object1, object2, object3)
class People(): def __init__(self, name): self.name = name def namePrint(self): print(self.name)person1 = People("Sally")person2 = People("Louise")person1.namePrint()A. person1 and person2 are two different instances of the People class.B. The __init__ method is used to set initial values for attributes.C. 'self' is not needed in def namePrint(self):D. person2 has a different value for 'name' than person1.
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.