English

Predict the output of the given class-attribute program. class Match: ' "Runs and Wickets" ' runs=281 wickets=5 def __init__(self,runs,wickets): self.runs=runs self.wickets=wickets print

Advertisements
Advertisements

Question

Predict the output of the given class-attribute program.

class Match:
     ' "Runs and Wickets" '
     runs=281
     wickets=5
     def __init__(self,runs,wickets):
          self.runs=runs
          self.wickets=wickets
     print " Runs scored are : ",runs
     print "Wickets taken are : ",wickets
print "Test.__do__:", Match.__doc__
print "Test.__name__:", Match.__name__
print "Test.__module__:", Match.__module__
print "Test.__bases__:", Match.__bases__
print "Test.__dict__:", Match.__dict__
Code Writing
Advertisements

Solution

The question uses Python 2 print syntax. In Python 3, it must be written using print().

No object of Match is created, so the messages about runs and wickets are not printed. The built-in class attributes produce the following values:

Match.__doc__: Runs and Wickets
Match.__name__: Match
Match.__module__: __main__
Match.__bases__: ()
Match.__dict__: mappingproxy({...})

Match.__dict__ contains the class namespace, including __module__, __doc__, runs, wickets and __init__. Its exact display is implementation-dependent because the function object includes a memory address.

class Match:
    """Runs and Wickets"""
    runs = 281
    wickets = 5

    def __init__(self, runs, wickets):
        self.runs = runs
        self.wickets = wickets
        print("Runs scored are:", runs)
        print("Wickets taken are:", wickets)


print("Match.__doc__:", Match.__doc__)
print("Match.__name__:", Match.__name__)
print("Match.__module__:", Match.__module__)
print("Match.__bases__:", Match.__bases__)
print("Match.__dict__:", Match.__dict__)
shaalaa.com
  Is there an error in this question or solution?
Chapter 3: Classes in Python - EXERCISE [Page 64]

APPEARS IN

CBSE Computer Science with Python [English] Class 12
Chapter 3 Classes in Python
EXERCISE | Q 19. | Page 64
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×