NEW
Font size
WorksheetsLearn Python 3 - Lesson 10 Introduction to Classes
Total questions: 7
Worksheet time: 7mins
What does the hasattr() function call in the last line here evaluate to?
class HoldsFive:
five = 5
five_holder = HoldsFive()
hasattr(five_holder, 'five')
True
False
5
What function, defined within a class, provides instructions on what to assign to a new instance when it is created?
__ init __
__ create __
init
__ new __
What is the first argument of a method?
The context in which the object is created. We usually name the parameter this.
The instance of the object itself. We usually refer to it as self.
The class itself. We usually refer to it as self.
What would be printed from the following code?
class User:
def __init__(self, name):
self.name = name
def __repr__(self):
return "Hiya {}!".format(self.name)
devorah = User("Devorah")
print(devorah)
Hiya Devorah!
Devorah
Hiya devorah!
devorah
What keyword is used to indicate the start of a class definition?
__ init __
def
class
type
What does the type() function do in Python?
Returns a type object that contains some metadata about the class.
Returns the class that an object implements.
Returns a string that’s the name of the class.
Returns an implementation of a class.
How would we create an instance of the following class?
class NiceClass:
neat_attribute = "neat"
nice_instance = NiceClass()
nice_instance = new NiceClass
nice_instance = NiceClass
