हिंदी

In the code given what will be output if the following command is given: (Assuming YC is the object) YC.display() class Yourclass marks=10 name= "ABC" def __init__(self,marks,name): self.marks=marks

Advertisements
Advertisements

प्रश्न

In the code given what will be output if the following command is given: (Assuming YC is the object) YC.display()

class Yourclass
       marks=10
       name= "ABC"
       def __init__(self,marks,name):
              self.marks=marks
              self.name=name
       def display(self):
              print marks
              print name
लघु उत्तरीय
Advertisements

उत्तर

The given display() method uses marks and name without self. Hence, in Python 3 it raises an error because these names are not local or global variables.

NameError: name 'marks' is not defined

Correct method:

def display(self):
    print(self.marks)
    print(self.name)

After correction, for YC = Yourclass(10, "ABC"), the output will be:

10
ABC
shaalaa.com
  क्या इस प्रश्न या उत्तर में कोई त्रुटि है?
अध्याय 3: Classes in Python - EXERCISE [पृष्ठ ६४]

APPEARS IN

सीबीएसई Computer Science with Python [English] Class 12
अध्याय 3 Classes in Python
EXERCISE | Q 18. | पृष्ठ ६४
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×