Stage 3 Notes

Juil's Udacity Intro to Programming Nanodegree

Lesson 1: Functions

Abstraction

You are able to use a function by reading the documentation without necessarily understanding how it works under the hood. This is abstraction.

Built-in Functions

Commonly used functions are built into the Pythong Standard Library and can be called without importing any modules.

Built-in Functions

Lesson 2: Classes & Modules

Classes

A class is a defined set of functions and properties that are applied to each instance of the class. brad = turtle.Turtle() is calling the def __init__() function in the class Turtle. This sets aside memory that wasn't taken previously.

Classes like random, os, and turtle are a part of the Python standard library. External libraries can be imported by calling:

sudo easy_install pip
sudo pip install twilio
import twilio
print twilio.__version__

Importing Modules

Any Python source file can be used as a module by executng an import statement in some other Python source file.

import module1[, module2[,... moduleN]

Import specific attributes from a module into the current namespace using from...import

from modname import name1[, name2[, ... nameN]]

Misc Vocabulary

  • Constructor - The initializing function that is caled when an instance is created. In python it is the `__init__` function.
  • Instance Variable - Class property set when an object is created, and accessible by calling instance.property It is defined by `self.property` in the `__init__` function.
  • Instance Method - Functions that can be called by instances and have `self` as a property. Otherwise, they are just local.

Lesson 3: Building Classes

Class

class Class():
    def __init__(self, var1[, var2,...[]]):
        self.var1 = var1

Class Variables

Class variables have values that can be accessed by every instance of that class.

Constants should be defined with all-caps.

CLASS_VARIABLE = [1, 2, 3]

Predefined Class Attributes

  • __doc__ - The class documentation string.
  • __name__ - The name of the class.
  • __module__ - The name of the module in which this class was defined.

Lesson 4: Advanced Class Concepts

Inheritance

A Child class can inherit variables and methods from a Parent class.

class Parent(object):
  def __init__(self, argP1, argP2):
    self.argP1 = argP1
    self.argP2 = argP2

class Child(Parent):
  def __init__(self, argP1, argP2, argC1):
    Parent.__init__(self, argP1, argP2)
    self.argC1 = argC1

class SuperChild(Parent):
  def __init__(self, argP1, argP2, argC1):
    super(SuperChild, self).__init__(argP1, argP2)
    self.argC1 = argC1

Using super().__init__() is a more reliable and forward compatible way of inheriting.

Inheritting Methods

Any instance methods in the parent class are available to child classes. The child class can also override an inheritted method by defining a new method with the same name. This is called method overriding.

Class and Static Method

A class method allows you to call a class within the function. The class should have @classmethodbefore it, and the first argument in a class method must be a call to the class itself.

A static method allows you to package related functions that don't need to be called on a class or instance. The class should have @staticmethod before it, and the arguments can be empty.

@classmethod
def get_instances(cls):
    yield cls.__refs__

@staticmethod
def get_allowable():
    return allowable