Python - Objects & Classes Flashcards
How to define a basic x-y Point class, with initialization method, default parameters, a new overrided add operator, overrided string method, class variables, etc
class Point:
# class variables (not instance variables) dimensions = 2
# this actually overrides the built-in \_\_init\_\_ method def \_\_init\_\_(self, x, y): self.x = x # instance variables self.y = y
# this overrides the default built-in str method # it returns the string that is printed out when you # return the \_\_init\_\_ object def \_\_str\_\_(self): return f'Point: ({self.x}, {self.y})'
# overriding the '+' operator def \_\_add\_\_(self,other): new_x = self.x + other.x new_y = self.y + other.y result = Point(new_x, new_y) return result
Define a child class Shuttle that inherits from the parent class Rocket
class Shuttle( Rocket ): def \_\_init\_\_(self, x=0, y=0, flights_completed=0): super().\_\_init\_\_(x, y) self.flights_completed = flights_completed
General template for a child class inheriting from parent class
class NewClass(ParentClass):
def \_\_init\_\_(self, arguments_new_class, arguments_parent_class): super().\_\_init\_\_(arguments_parent_class)
Create several shuttles and rockets, with random positions (instantiate multiple objects at once)
Shuttles have a random number of flights completed.
class Shuttle(Rocket): # Shuttle simulates a space shuttle, which is really # just a reusable rocket.
def \_\_init\_\_(self, x=0, y=0, flights_completed=0): super().\_\_init\_\_(x, y) self.flights_completed = flights_completed
##############
shuttles = [] for x in range(0,3): x = randint(0,100) y = randint(1,100) flights_completed = randint(0,10) shuttles.append(Shuttle(x, y, flights_completed))
rockets = [] for x in range(0,3): x = randint(0,100) y = randint(1,100) rockets.append(Rocket(x, y))
What’s a module? How does this relate to your knowledge of classes?
Now that you are starting to work with classes, your files are going to grow longer. Python allows you to save your classes in another file and then import them into the program you are working on.
When you save a class into a separate file, that file is called a module. You can have any number of classes in a single module.
Create a new module based on the class Rocket(). Then create a new file that imports this module. What does this look like?
(The first line tells Python to look for a file called rocket.py. It looks for that file in the same directory as your current program)
(When Python finds the file rocket.py, it looks for a class called Rocket. When it finds that class, it imports that code into the current file, without you ever seeing that code)
Save as rocket.py
class Rocket(): # Rocket simulates a rocket ship for a game, # or a physics simulation.
def \_\_init\_\_(self, x=0, y=0): # Each rocket has an (x,y) position. self.x = x self.y = y
\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_ # This is a new file: # Save as rocket_game.py
from rocket import Rocket
rocket = Rocket()
print(“The rocket is at (%d, %d).” % (rocket.x, rocket.y))
You can also have multiple classes within the same module file. How would you write this for both the Rocket() and Shuttle() classes?
Save as rocket.py
class Rocket(): # Rocket simulates a rocket ship for a game, # or a physics simulation.
def \_\_init\_\_(self, x=0, y=0): # Each rocket has an (x,y) position. self.x = x self.y = y
class Shuttle(Rocket): # Shuttle simulates a space shuttle, which is really # just a reusable rocket.
def \_\_init\_\_(self, x=0, y=0, flights_completed=0): super().\_\_init\_\_(x, y) self.flights_completed = flights_completed
\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_ # Save as rocket_game.py
from rocket import Rocket, Shuttle
rocket = Rocket() shuttle = Shuttle()
List 4 different ways for how you can import modules.
from module_name import ClassName/function
________
import module_name
#After this, classes are accessed using dot notation:
#module_name.ClassName
________
import module_name as local_module_name
#You can access this now using the alias given
________
from module_name import *
This is not recommended, for a couple reasons. First of all, you may have no idea what all the names of the classes and functions in a module are. If you accidentally give one of your variables the same name as a name from the module, you will have naming conflicts. Also, you may be importing way more code into your program than you need.
You can also store functions in a module. How would you go about writing this module and using it?
# Save as multiplying.py def double(x): return 2*x
def triple(x): return 3*x
def quadruple(x): return 4*x \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_ # you can import it and use it this way
from multiplying import double, triple, quadruple
print(double(5))
print(triple(5))
print(quadruple(5))
\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_ # or you can import and use it this way:
import multiplying
print(multiplying.double(5)) print(multiplying.triple(5)) print(multiplying.quadruple(5)) \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_ # or you can alsp import and use it this way:
from multiplying import *
print(double(5))
print(triple(5))
print(quadruple(5))
What are the PEP8 guidelines for writing names for classes and modules?
Modules should have short, lowercase names. If you want to have a space in the module name, use an underscore.
Class names should be written in CamelCase, with an initial capital letter and any new word capitalized. There should be no underscores in your class names.
This convention helps distinguish modules from classes, for example when you are writing import statements.
How is the dot . operator overloaded?
The dot . operator is overloaded in Python to mean both package member and object member access. You are familiar with this already:
import numpy as np
np.array([1,2,3])
versus
import math
math.log(3000)
This is a common point of confusion when reading code. When we see a.f(), we don’t know whether that function f is a member of the package identified by a or an object referred to by a.