SlideShare una empresa de Scribd logo
1 de 15
Descargar para leer sin conexión
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

Más contenido relacionado

La actualidad más candente

Computer programming(C++): Structures
Computer programming(C++): StructuresComputer programming(C++): Structures
Computer programming(C++): StructuresJishnuNath7
 
Ms Ajax String And Object Extensions
Ms Ajax String And Object ExtensionsMs Ajax String And Object Extensions
Ms Ajax String And Object Extensionsjason hu 金良胡
 
C# Summer course - Lecture 4
C# Summer course - Lecture 4C# Summer course - Lecture 4
C# Summer course - Lecture 4mohamedsamyali
 
Variables 9 cm604.7
Variables 9 cm604.7Variables 9 cm604.7
Variables 9 cm604.7myrajendra
 
Advanced php
Advanced phpAdvanced php
Advanced phphamfu
 
Java string , string buffer and wrapper class
Java string , string buffer and wrapper classJava string , string buffer and wrapper class
Java string , string buffer and wrapper classSimoniShah6
 
20170113 julia’s type system and multiple dispatch
20170113 julia’s type system and multiple dispatch20170113 julia’s type system and multiple dispatch
20170113 julia’s type system and multiple dispatch岳華 杜
 
Strings In OOP(Object oriented programming)
Strings In OOP(Object oriented programming)Strings In OOP(Object oriented programming)
Strings In OOP(Object oriented programming)Danial Virk
 
Ios development
Ios developmentIos development
Ios developmentelnaqah
 
Introduce oop in python
Introduce oop in pythonIntroduce oop in python
Introduce oop in pythontuan vo
 
The Ruby Object Model by Rafael Magana
The Ruby Object Model by Rafael MaganaThe Ruby Object Model by Rafael Magana
The Ruby Object Model by Rafael MaganaRafael Magana
 

La actualidad más candente (20)

Computer programming(C++): Structures
Computer programming(C++): StructuresComputer programming(C++): Structures
Computer programming(C++): Structures
 
Oop concepts in python
Oop concepts in pythonOop concepts in python
Oop concepts in python
 
Ms Ajax String And Object Extensions
Ms Ajax String And Object ExtensionsMs Ajax String And Object Extensions
Ms Ajax String And Object Extensions
 
C# Summer course - Lecture 4
C# Summer course - Lecture 4C# Summer course - Lecture 4
C# Summer course - Lecture 4
 
Introduction to php oop
Introduction to php oopIntroduction to php oop
Introduction to php oop
 
package
packagepackage
package
 
Variables 9 cm604.7
Variables 9 cm604.7Variables 9 cm604.7
Variables 9 cm604.7
 
Python programming : Inheritance and polymorphism
Python programming : Inheritance and polymorphismPython programming : Inheritance and polymorphism
Python programming : Inheritance and polymorphism
 
Advanced php
Advanced phpAdvanced php
Advanced php
 
Python3
Python3Python3
Python3
 
Java string , string buffer and wrapper class
Java string , string buffer and wrapper classJava string , string buffer and wrapper class
Java string , string buffer and wrapper class
 
Python oop third class
Python oop   third classPython oop   third class
Python oop third class
 
Python classes objects
Python classes objectsPython classes objects
Python classes objects
 
20170113 julia’s type system and multiple dispatch
20170113 julia’s type system and multiple dispatch20170113 julia’s type system and multiple dispatch
20170113 julia’s type system and multiple dispatch
 
Strings In OOP(Object oriented programming)
Strings In OOP(Object oriented programming)Strings In OOP(Object oriented programming)
Strings In OOP(Object oriented programming)
 
Ios development
Ios developmentIos development
Ios development
 
Python: Basic Inheritance
Python: Basic InheritancePython: Basic Inheritance
Python: Basic Inheritance
 
Introduce oop in python
Introduce oop in pythonIntroduce oop in python
Introduce oop in python
 
The Ruby Object Model by Rafael Magana
The Ruby Object Model by Rafael MaganaThe Ruby Object Model by Rafael Magana
The Ruby Object Model by Rafael Magana
 
Python - OOP Programming
Python - OOP ProgrammingPython - OOP Programming
Python - OOP Programming
 

Similar a Anton Kasyanov, Introduction to Python, Lecture5

My Object Oriented.pptx
My Object Oriented.pptxMy Object Oriented.pptx
My Object Oriented.pptxGopalNarayan7
 
جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲Mohammad Reza Kamalifard
 
Chap 3 Python Object Oriented Programming - Copy.ppt
Chap 3 Python Object Oriented Programming - Copy.pptChap 3 Python Object Oriented Programming - Copy.ppt
Chap 3 Python Object Oriented Programming - Copy.pptmuneshwarbisen1
 
Python programming computer science and engineering
Python programming computer science and engineeringPython programming computer science and engineering
Python programming computer science and engineeringIRAH34
 
Introduction to Python - Part Three
Introduction to Python - Part ThreeIntroduction to Python - Part Three
Introduction to Python - Part Threeamiable_indian
 
Python Classes_ Empowering Developers, Enabling Breakthroughs.pdf
Python Classes_ Empowering Developers, Enabling Breakthroughs.pdfPython Classes_ Empowering Developers, Enabling Breakthroughs.pdf
Python Classes_ Empowering Developers, Enabling Breakthroughs.pdfSudhanshiBakre1
 
Python Classes_ Empowering Developers, Enabling Breakthroughs.pdf
Python Classes_ Empowering Developers, Enabling Breakthroughs.pdfPython Classes_ Empowering Developers, Enabling Breakthroughs.pdf
Python Classes_ Empowering Developers, Enabling Breakthroughs.pdfSudhanshiBakre1
 
Java 2 chapter 10 - basic oop in java
Java 2   chapter 10 - basic oop in javaJava 2   chapter 10 - basic oop in java
Java 2 chapter 10 - basic oop in javalet's go to study
 
packages and interfaces
packages and interfacespackages and interfaces
packages and interfacesmadhavi patil
 
Polymorphism.pptx
Polymorphism.pptxPolymorphism.pptx
Polymorphism.pptxVijaykota11
 
Presentation 3rd
Presentation 3rdPresentation 3rd
Presentation 3rdConnex
 

Similar a Anton Kasyanov, Introduction to Python, Lecture5 (20)

My Object Oriented.pptx
My Object Oriented.pptxMy Object Oriented.pptx
My Object Oriented.pptx
 
Unit - 3.pptx
Unit - 3.pptxUnit - 3.pptx
Unit - 3.pptx
 
جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
 
Chap 3 Python Object Oriented Programming - Copy.ppt
Chap 3 Python Object Oriented Programming - Copy.pptChap 3 Python Object Oriented Programming - Copy.ppt
Chap 3 Python Object Oriented Programming - Copy.ppt
 
Python programming computer science and engineering
Python programming computer science and engineeringPython programming computer science and engineering
Python programming computer science and engineering
 
Introduction to Python - Part Three
Introduction to Python - Part ThreeIntroduction to Python - Part Three
Introduction to Python - Part Three
 
Pythonclass
PythonclassPythonclass
Pythonclass
 
Reflection
ReflectionReflection
Reflection
 
Python Classes_ Empowering Developers, Enabling Breakthroughs.pdf
Python Classes_ Empowering Developers, Enabling Breakthroughs.pdfPython Classes_ Empowering Developers, Enabling Breakthroughs.pdf
Python Classes_ Empowering Developers, Enabling Breakthroughs.pdf
 
Python Classes_ Empowering Developers, Enabling Breakthroughs.pdf
Python Classes_ Empowering Developers, Enabling Breakthroughs.pdfPython Classes_ Empowering Developers, Enabling Breakthroughs.pdf
Python Classes_ Empowering Developers, Enabling Breakthroughs.pdf
 
Spsl vi unit final
Spsl vi unit finalSpsl vi unit final
Spsl vi unit final
 
Spsl v unit - final
Spsl v unit - finalSpsl v unit - final
Spsl v unit - final
 
Ch-2ppt.pptx
Ch-2ppt.pptxCh-2ppt.pptx
Ch-2ppt.pptx
 
Java 2 chapter 10 - basic oop in java
Java 2   chapter 10 - basic oop in javaJava 2   chapter 10 - basic oop in java
Java 2 chapter 10 - basic oop in java
 
python.pptx
python.pptxpython.pptx
python.pptx
 
python note.pdf
python note.pdfpython note.pdf
python note.pdf
 
packages and interfaces
packages and interfacespackages and interfaces
packages and interfaces
 
Python session 7 by Shan
Python session 7 by ShanPython session 7 by Shan
Python session 7 by Shan
 
Polymorphism.pptx
Polymorphism.pptxPolymorphism.pptx
Polymorphism.pptx
 
Presentation 3rd
Presentation 3rdPresentation 3rd
Presentation 3rd
 

Más de Anton Kasyanov

spaCy lightning talk for KyivPy #21
spaCy lightning talk for KyivPy #21spaCy lightning talk for KyivPy #21
spaCy lightning talk for KyivPy #21Anton Kasyanov
 
Introduction to Computer Vision (uapycon 2017)
Introduction to Computer Vision (uapycon 2017)Introduction to Computer Vision (uapycon 2017)
Introduction to Computer Vision (uapycon 2017)Anton Kasyanov
 
Anton Kasyanov, Introduction to Python, Lecture3
Anton Kasyanov, Introduction to Python, Lecture3Anton Kasyanov, Introduction to Python, Lecture3
Anton Kasyanov, Introduction to Python, Lecture3Anton Kasyanov
 
Anton Kasyanov, Introduction to Python, Lecture2
Anton Kasyanov, Introduction to Python, Lecture2Anton Kasyanov, Introduction to Python, Lecture2
Anton Kasyanov, Introduction to Python, Lecture2Anton Kasyanov
 
Anton Kasyanov, Introduction to Python, Lecture1
Anton Kasyanov, Introduction to Python, Lecture1Anton Kasyanov, Introduction to Python, Lecture1
Anton Kasyanov, Introduction to Python, Lecture1Anton Kasyanov
 
Anton Kasyanov, Introduction to Python, Lecture4
Anton Kasyanov, Introduction to Python, Lecture4Anton Kasyanov, Introduction to Python, Lecture4
Anton Kasyanov, Introduction to Python, Lecture4Anton Kasyanov
 

Más de Anton Kasyanov (7)

spaCy lightning talk for KyivPy #21
spaCy lightning talk for KyivPy #21spaCy lightning talk for KyivPy #21
spaCy lightning talk for KyivPy #21
 
Introduction to Computer Vision (uapycon 2017)
Introduction to Computer Vision (uapycon 2017)Introduction to Computer Vision (uapycon 2017)
Introduction to Computer Vision (uapycon 2017)
 
aiohttp intro
aiohttp introaiohttp intro
aiohttp intro
 
Anton Kasyanov, Introduction to Python, Lecture3
Anton Kasyanov, Introduction to Python, Lecture3Anton Kasyanov, Introduction to Python, Lecture3
Anton Kasyanov, Introduction to Python, Lecture3
 
Anton Kasyanov, Introduction to Python, Lecture2
Anton Kasyanov, Introduction to Python, Lecture2Anton Kasyanov, Introduction to Python, Lecture2
Anton Kasyanov, Introduction to Python, Lecture2
 
Anton Kasyanov, Introduction to Python, Lecture1
Anton Kasyanov, Introduction to Python, Lecture1Anton Kasyanov, Introduction to Python, Lecture1
Anton Kasyanov, Introduction to Python, Lecture1
 
Anton Kasyanov, Introduction to Python, Lecture4
Anton Kasyanov, Introduction to Python, Lecture4Anton Kasyanov, Introduction to Python, Lecture4
Anton Kasyanov, Introduction to Python, Lecture4
 

Último

Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024Janet Corral
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 

Último (20)

Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 

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