Challenge 1
Declare name and active as the only supported instance attributes.
class User:
user = User()
user.name = "Maya"
user.active = True
print(user.name, user.active)
Press Ctrl⌘Enter to check your answer
Practice declaring slots, restricting attributes, allowing a dictionary explicitly, and using slots across inheritance.
Challenge 1
class User:
user = User()
user.name = "Maya"
user.active = True
print(user.name, user.active)
Press Ctrl⌘Enter to check your answer
Challenge 2
class User:
__slots__ = ("name",
user = User()
user.name = "Maya"
user.color = "blue"
print(user.__dict__)
Press Ctrl⌘Enter to check your answer
Challenge 3
class Parent:
__slots__ = ("name",)
class Child(Parent):
child = Child()
child.name = "Ada"
child.score = 100
print(child.name, child.score)
Press Ctrl⌘Enter to check your answer
Challenge 4
class Counter:
counter = Counter()
counter.count = 1
try:
counter.label = "items"
except AttributeError:
print("label not allowed")
Press Ctrl⌘Enter to check your answer