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...
Classes   To make a class we just do:      class Class_name(object):           block   Class is a keyword and object is ...
Methods   To create a method we use the keyword self   Say we have a class Patient that is meant to    store information...
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...
Constructors   There is also a way to set class variables when    you construct class instances.   The special method __...
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   ...
Structure   If we need just a simple structure, we can use    this:       class Employee:           pass        john = ...
Naming conventions   Class names start with upper case letters.   Class methods and instances start with lower    case l...
Inheritance   The syntax for creating subclasses is:   class Class_name(Superclass_name):         block   Note that thi...
Lambda functions   Lambda functions are very simple one-line    functions that can be treated as an expression.   Syntax...
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
Upcoming SlideShare
Loading in …5
×

Anton Kasyanov, Introduction to Python, Lecture5

215 views

Published on

Published in: Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
215
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
1
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Anton Kasyanov, Introduction to Python, Lecture5

  1. 1. Introduction to Python Lecture 5 Kasyanov Anton IASA 2011
  2. 2. Plan Classes Tips
  3. 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. 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. 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 patients age. To do this we need a method set_age. class Patient(object): def set_age(self, age): self.age = age
  6. 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. 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. 8. Constructors Using constructors we can also init variables:  def __init__(self):  self.data = []
  9. 9. Builtin methods __ indicates that the method is a special method. These are used to make our classes work more like Pythons 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. 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. 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. 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. 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. 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. 15. Summary Please, fill in the form about course quality http://bit.ly/rWLaZ5

×