Final test Flashcards
(25 cards)
What is a class in Python?
It is a “blueprint” for creating objects, encapsulating data (attributes) and behavior (methods) into a single unit.
What is the purpose of the __init__ method in a class?
It initializes the attributes of an object when it is made.
What is an attribute?
It is a characteristic of an object.
What is a method?
It is a function that is associated with an object.
How do you create an instance of a class?
You call the class like a function.
How do you access an object’s attributes from outside the class using dot notation?
You need to write the object’s name,then a dot, and after that, the name of the attribute you want to access.
How do you call an object’s methods from outside the class using dot notation?
First, an object of the class needs to be made. After that, the method is called by writing the object’s name with a dot, and then the method’s name, along with any needed arguments in parentheses.
How do you access an object’s attributes from inside the class using dot notation?
You need the self keyword
How do you call an object’s methods from inside the class using dot notation?
You use self with a dot, and the method name.
What attributes does a Pet have?
self.name
self.energy
self.hunger
self.alive
self.x
self.y
self.direction
What are the default values for each attribute?
True, 0
What are two ways to create a Pet? One should put the pet in its default location. The other should put it at an (x, y) location that is not the origin
Gary = pet(“Gary”)
Gary = (“pet”, “Gary”, 8, 2)
How are pet directions represented internally?
0 1 2 and 3
How do the turn methods ensure that the Pet’s direction attribute does not get assigned an invalid value?
It adds numbers until it gets to a specific number then it restarts the numbers.
How far does a Pet move each time its move() method is called?
1 pixel
How is the direction a Pet moves determined when its move() method is called?
By a keystroke
Which methods modify the Pet’s energy attribute?
Eat, sleep, and play
Which methods modify the Pet’s hunger attribute?
Eat and play
What is the range of possible values for energy? For hunger?
It can be from 0 to any number for hunger and 0 to 10 for energy.
How/where are guard clauses used to prevent the Pet from reaching invalid states?
It checks to see if the pet is dead then it stops the function if the pet is dead.
In kill(), what are the meanings of self and other?
Self is the one doing the killing and other is the one being killed
When should we put single blank lines in our code?
To separate things within a function
When should we put double blank lines in our code?
To separate different functions
What are the proper naming conventions for classes, variables, methods, and constants in Python?
Camel case for classes, snake_case for variables and methods, and constants in ALL_CAPS.