0
0
Pythonprogramming~10 mins

Self reference in Python - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to make the method return the instance itself.

Python
class Box:
    def get_self(self):
        return [1]
Drag options to blanks, or click blank then click option'
Aself
BBox
Cthis
Dinstance
Attempts:
3 left
💡 Hint
Common Mistakes
Returning the class name instead of the instance.
Using 'this' which is not a Python keyword.
2fill in blank
medium

Complete the code to set an attribute on the instance using self reference.

Python
class Person:
    def __init__(self, name):
        [1].name = name
Drag options to blanks, or click blank then click option'
APerson
Bthis
Ccls
Dself
Attempts:
3 left
💡 Hint
Common Mistakes
Using the class name instead of the instance.
Using 'cls' which is for class methods.
3fill in blank
hard

Fix the error in the method to return the instance's attribute using self reference.

Python
class Car:
    def __init__(self, model):
        self.model = model
    def get_model(self):
        return [1].model
Drag options to blanks, or click blank then click option'
Aself
BCar
Ccls
Dmodel
Attempts:
3 left
💡 Hint
Common Mistakes
Using the class name instead of the instance.
Trying to access attribute directly without self.
4fill in blank
hard

Fill both blanks to create a method that returns a new instance referencing the current instance's attribute.

Python
class Node:
    def __init__(self, value):
        self.value = value
    def copy(self):
        return [1](self.[2])
Drag options to blanks, or click blank then click option'
ANode
Bvalue
Cself
Dcopy
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'copy' as a class name.
Using 'value' instead of 'self' to access attribute.
5fill in blank
hard

Fill all three blanks to define a method that updates the instance's attribute and returns the instance itself.

Python
class Counter:
    def __init__(self, count=0):
        self.count = count
    def increment(self, amount):
        [1].count += [2]
        return [3]
Drag options to blanks, or click blank then click option'
Aself
Bamount
Dcount
Attempts:
3 left
💡 Hint
Common Mistakes
Returning amount instead of the instance.
Using the attribute name instead of self.