More on this book
Community
Kindle Notes & Highlights
by
Eric Matthes
Started reading
November 1, 2018
Sometimes you’ll want to prevent a function from modifying a
You can send a copy of a list to a function like this:
function_name(list_name[:])
The slice notation [:]
def make_pizza(*toppings):
def build_profile(first, last, **user_info):
follow. You can go a step further by storing your functions in a separate file called a module and then importing that module into your main
A module is a file ending in .py
Importing Specific Functions
from module_name import function_name
Using as to Give a Function an Alias
If the name of a function you’re importing might conflict with an existing name in your program or
Using as to Give a Module an Alias
Making an object from a class is called instantiation,
Creating the Dog Class
➌ def __init__(self, name, age): """Initialize name and age
__init__() Method
When Python reads this line, it calls the __init__() method
➊ self.odometer_reading = 0
the class you’re writing is a specialized version of another class you
inherits from another,
➊ class Car(): """A simple attempt to represent a car."""
class ElectricCar(Car):
super().__init__(make, model, year)
The name of the parent class must be included in parentheses in the definition of the child class.
The super() function at ➍ is a special function that helps Python make connections between the parent and child class.
The name super comes from a convention of calling the parent class a superclass
class Car(object):
class ElectricCar(Car):
def __init__(self, make, model, year):
super(ElectricCar, self).__init__(make...
This highlight has been truncated due to consecutive passage length restrictions.
super() function needs two arguments: a reference to the child class and the self object.
make proper connections between the parent and child classes.
object syntax as well.
Overriding
def fill_gas_tank(self): """Electric cars don't have gas tanks.""" print("This car doesn't need a gas tank!")
the ElectricCar class, we might notice that we’re adding many attributes and methods specific to the car’s battery.
Importing Classes
from car import Car
Storing Multiple Classes in a Module You can store as many classes as you need in a single module, although each class in a module should be related somehow.
from car import Car, ElectricCar
Importing an Entire Module
➊ import car
from module_name import *
When you’re starting out, keep your code structure simple. Try doing everything in one file and moving your classes to separate modules once everything is working.
The Python Standard Library
Python standard library is a set of modules included with every Python installation.
Styling Classes
in CamelCaps.
with open('pi_digits.txt') as file_object:

