Introduction to Python Lecture 5 Kasyanov Anton IASA 2011
Plan  Classes  Tips
What are classes?  Python allows us to build our own types.  These are called classes.  When we have a class, we can create instances/objects of that class.  Recall the difference between a type (str, int) and a value ('this is a string', 10).  This is analogous to the difference between a class and an object.
Classes  To make a class we just do: class Class_name(object): block  Class is a keyword and object is a type.  Class names start with Capital letters by convention.  To create objects or instances of Class_name we use: x = Class_name()
Methods  To create a method we use the keyword self  Say we have a class Patient that is meant to store information about patients in a hospital.  We may want to update the patient's age.  To do this we need a method set_age. class Patient(object): def set_age(self, age): self.age = age
Methods  Note that these are methods, so that if we have a patient p1 to set his age we use p1.set_age(10) and not p1.set_age(p1,10)  Self should always be the first parameter in a method but it is passed automatically when we call the method.
Constructors  There is also a way to set class variables when you construct class instances.  The special method __init__ is called whenever you try and construct an instance of an object. class Patient(object): def __init__(self, name, age): self.name = name self.age = age  Called by x = Patient(“Joey”, 10)
Constructors  Using constructors we can also init variables:  def __init__(self):  self.data = []
Builtin methods  __ indicates that the method is a special method.  These are used to make our classes work more like Python's built-in types.  For example:  __str__ is used when printing  __cmp__ is used to allow boolean operations.  __add__ is used to allow the + operator.  __iter__ is used to allow your type to be used in for loops. 
Structure  If we need just a simple structure, we can use this:  class Employee:  pass john = Employee() # Create an empty employee record  # Fill the fields of the record  john.name = 'John Doe'  john.dept = 'computer lab'  john.salary = 1000
Naming conventions  Class names start with upper case letters.  Class methods and instances start with lower case letters.  Method definitions should have docstrings just like function definitions.  Classes should have docstrings just like modules have docstrings that describe what the class does.
Inheritance  The syntax for creating subclasses is:  class Class_name(Superclass_name): block  Note that this means our previous class is a subclass of the class object.  If you define a method with the same name as one in the superclass, you overwrite it.
Lambda functions  Lambda functions are very simple one-line functions that can be treated as an expression.  Syntax: lambda arg1,arg2,... : expr   def add(a,b): return a+b  add2 = lambda a,b: a+b
List comprehensions  One line way to create a list  E.g.: [a*2 for a in [1,2,3]]  Returns:  [2,4,6]
Summary  Please, fill in the form about course quality  http://bit.ly/rWLaZ5

Anton Kasyanov, Introduction to Python, Lecture5

  • 1.
    Introduction to Python Lecture 5 Kasyanov Anton IASA 2011
  • 2.
    Plan  Classes  Tips
  • 3.
    What are classes?  Python allows us to build our own types.  These are called classes.  When we have a class, we can create instances/objects of that class.  Recall the difference between a type (str, int) and a value ('this is a string', 10).  This is analogous to the difference between a class and an object.
  • 4.
    Classes  To make a class we just do: class Class_name(object): block  Class is a keyword and object is a type.  Class names start with Capital letters by convention.  To create objects or instances of Class_name we use: x = Class_name()
  • 5.
    Methods  To create a method we use the keyword self  Say we have a class Patient that is meant to store information about patients in a hospital.  We may want to update the patient's age.  To do this we need a method set_age. class Patient(object): def set_age(self, age): self.age = age
  • 6.
    Methods  Note that these are methods, so that if we have a patient p1 to set his age we use p1.set_age(10) and not p1.set_age(p1,10)  Self should always be the first parameter in a method but it is passed automatically when we call the method.
  • 7.
    Constructors  There is also a way to set class variables when you construct class instances.  The special method __init__ is called whenever you try and construct an instance of an object. class Patient(object): def __init__(self, name, age): self.name = name self.age = age  Called by x = Patient(“Joey”, 10)
  • 8.
    Constructors  Using constructors we can also init variables:  def __init__(self):  self.data = []
  • 9.
    Builtin methods  __ indicates that the method is a special method.  These are used to make our classes work more like Python's built-in types.  For example:  __str__ is used when printing  __cmp__ is used to allow boolean operations.  __add__ is used to allow the + operator.  __iter__ is used to allow your type to be used in for loops. 
  • 10.
    Structure  If we need just a simple structure, we can use this:  class Employee:  pass john = Employee() # Create an empty employee record  # Fill the fields of the record  john.name = 'John Doe'  john.dept = 'computer lab'  john.salary = 1000
  • 11.
    Naming conventions  Class names start with upper case letters.  Class methods and instances start with lower case letters.  Method definitions should have docstrings just like function definitions.  Classes should have docstrings just like modules have docstrings that describe what the class does.
  • 12.
    Inheritance  The syntax for creating subclasses is:  class Class_name(Superclass_name): block  Note that this means our previous class is a subclass of the class object.  If you define a method with the same name as one in the superclass, you overwrite it.
  • 13.
    Lambda functions  Lambda functions are very simple one-line functions that can be treated as an expression.  Syntax: lambda arg1,arg2,... : expr   def add(a,b): return a+b  add2 = lambda a,b: a+b
  • 14.
    List comprehensions  One line way to create a list  E.g.: [a*2 for a in [1,2,3]]  Returns:  [2,4,6]
  • 15.
    Summary  Please, fill in the form about course quality  http://bit.ly/rWLaZ5