Skills101
← All practice quizzes

Quiz: Magic Methods and Properties

5 questions · new set on every retakeNo time limitPass: 60%+10 XP on first pass

Make your own classes behave like built-in types. repr, eq, lt, len, getitem and add, plus @property for safe attributes and the difference between classmethod and staticmethod.

Log in to take the quiz

Free account · see your score, answers and explanations, earn XP

  1. Question 1

    Which method does len(obj) call?

  2. Question 2

    What does this print?

    class P:
        def __repr__(self):
            return "P()"
    print([P(), P()])
    
  3. Question 3

    If a class defines only __repr__, what does print(obj) use?

  4. Question 4

    Without __eq__, what does a == b check for two objects of your class?

  5. Question 5

    What does this print?

    class V:
        def __init__(self, x):
            self.x = x
        def __add__(self, o):
            return V(self.x + o.x)
    print((V(2) + V(5)).x)
    
🎯 Today’s placement challenge — 10 questions, no login. Can you score 10/10?Take today’s challenge →