SlideShare une entreprise Scribd logo
1  sur  50
Python Tutorial

Haitham El-Ghareeb, Ph.D.
        May, 2012
  Twitter: @helghareeb
Modules
• A module is a file containing Python
  definitions and statements.
• The file name is the module name with the
  suffix .py appended.
• Within a module, the module’s name (as a
  string) is available as the value of the global
  variable __name__.
Assign Local Name
Compiled Python Files
• As an important speed-up of the start-up time for
  short programs that use a lot of standard
  modules,
• if a file called spam.pyc exists in the directory
  where spam.py is found, this is assumed to
  contain an already-“byte-compiled” version of
  the module spam.
• The modification time of the version of spam.py
  used to create spam.pyc is recorded in spam.pyc,
  and the .pyc file is ignored if these don’t match.
Compiled Python Files
• Normally, you don’t need to do anything to create the
  spam.pyc file.
• Whenever spam.py is successfully compiled, an
  attempt is made to write the compiled version to
  spam.pyc.
• It is not an error if this attempt fails; if for any reason
  the file is not written completely, the resulting
  spam.pyc file will be recognized as invalid and thus
  ignored later.
• The contents of the spam.pyc file are platform
  independent, so a Python module directory can be
  shared by machines of different architectures.
Some Tips for Experts
• When the Python interpreter is invoked with
  the -O flag, optimized code is generated and
  stored in .pyo files.
• The optimizer currently doesn’t help much; it
  only removes assert statements.
• When -O is used, all bytecode is optimized;
  .pyc files are ignored and .py files are
  compiled to optimized bytecode.
Some Tips for Experts
• Passing two -O flags to the Python interpreter (-
  OO) will cause the bytecode compiler to perform
  optimizations that could in some rare cases result
  in malfunctioning programs.
• Currently only __doc__ strings are removed from
  the bytecode, resulting in more compact .pyo
  files.
• Since some programs may rely on having these
  available, you should only use this option if you
  know what you’re doing.
Some Tips for Experts
• A program doesn’t run any faster when it is
  read from a .pyc or .pyo file than when it is
  read from a .py file; the only thing that’s faster
  about .pyc or .pyo files is the speed with
  which they are loaded.
Some Tips for Experts
• When a script is run by giving its name on the
  command line, the bytecode for the script is
  never written to a .pyc or .pyo file.
• Thus, the startup time of a script may be
  reduced by moving most of its code to a
  module and having a small bootstrap script
  that imports that module. It is also possible to
  name a .pyc or .pyo file directly on the
  command line.
Some Tips for Experts
• It is possible to have a file called spam.pyc (or
  spam.pyo when -O is used) without a file
  spam.py for the same module.
• This can be used to distribute a library of
  Python code in a form that is moderately hard
  to reverse engineer.
Some Tips for Experts
• The module compileall can create .pyc files (or
  .pyo files when -O is used) for all modules in a
  directory.
Standard Modules
Dir()
• The built-in function dir() is used to find out
  which names a module defines. It returns a
  sorted list of strings.
Packages
• Packages are a way of structuring Python’s module
  namespace by using “dotted module names”.
• For example, the module name A.B designates a
  submodule named B in a package named A.
• Just like the use of modules saves the authors of
  different modules from having to worry about each
  other’s global variable names, the use of dotted
  module names saves the authors of multi-module
  packages like NumPy or the Python Imaging Library
  from having to worry about each other’s module
  names.
Input and Output
• There are several ways to present the output
  of a program; data can be printed in a human-
  readable form, or written to a file for future
  use. This chapter will discuss some of the
  possibilities.
• How do you convert values to strings?
Representation
• The str() function is meant to return
  representations of values which are fairly
  human-readable.
• repr() is meant to generate representations
  which can be read by the interpreter (or will
  force a SyntaxError if there is not equivalent
  syntax).
Str() and Repr()
Formatting Output
Formatting Output
I don’t think so!

MORE ON STRING FORMATTING?
Reading and Writing Files
• Open() returns a file object
Reading and Writing Files
Alternative Method
Write to File
Close
Pickle Module
Errors and Exceptions
• Syntax Errors
Exceptions
Handling Exceptions
Else?!
Another Exception
Raise Exception
Catching it!
User Defined Exception
Clean Up Actions
Classes
Class Definition Syntax
Class Objects
Inheritance
Multiple Inheritance
Iterators
Style of Access
Make your Class Iterable
Len()
• Implement __len__
Unit Testing
• Hopefully!

Contenu connexe

Tendances

Tendances (20)

Introduction to python programming, Why Python?, Applications of Python
Introduction to python programming, Why Python?, Applications of PythonIntroduction to python programming, Why Python?, Applications of Python
Introduction to python programming, Why Python?, Applications of Python
 
Python PPT
Python PPTPython PPT
Python PPT
 
Python Course | Python Programming | Python Tutorial | Python Training | Edureka
Python Course | Python Programming | Python Tutorial | Python Training | EdurekaPython Course | Python Programming | Python Tutorial | Python Training | Edureka
Python Course | Python Programming | Python Tutorial | Python Training | Edureka
 
Basics of python
Basics of pythonBasics of python
Basics of python
 
Presentation on python
Presentation on pythonPresentation on python
Presentation on python
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to python
 
Python programming | Fundamentals of Python programming
Python programming | Fundamentals of Python programming Python programming | Fundamentals of Python programming
Python programming | Fundamentals of Python programming
 
Python Tutorial | Python Tutorial for Beginners | Python Training | Edureka
Python Tutorial | Python Tutorial for Beginners | Python Training | EdurekaPython Tutorial | Python Tutorial for Beginners | Python Training | Edureka
Python Tutorial | Python Tutorial for Beginners | Python Training | Edureka
 
Overview of python 2019
Overview of python 2019Overview of python 2019
Overview of python 2019
 
Python basics
Python basicsPython basics
Python basics
 
Python Presentation
Python PresentationPython Presentation
Python Presentation
 
Python final presentation kirti ppt1
Python final presentation kirti ppt1Python final presentation kirti ppt1
Python final presentation kirti ppt1
 
Python tutorial
Python tutorialPython tutorial
Python tutorial
 
Python libraries
Python librariesPython libraries
Python libraries
 
Python Basics
Python BasicsPython Basics
Python Basics
 
Intro to Python Programming Language
Intro to Python Programming LanguageIntro to Python Programming Language
Intro to Python Programming Language
 
Variables & Data Types In Python | Edureka
Variables & Data Types In Python | EdurekaVariables & Data Types In Python | Edureka
Variables & Data Types In Python | Edureka
 
Python final ppt
Python final pptPython final ppt
Python final ppt
 
Python basics
Python basicsPython basics
Python basics
 
Python programming
Python  programmingPython  programming
Python programming
 

En vedette

En vedette (20)

Python Tutorial Part 1
Python Tutorial Part 1Python Tutorial Part 1
Python Tutorial Part 1
 
Basics of Python programming (part 2)
Basics of Python programming (part 2)Basics of Python programming (part 2)
Basics of Python programming (part 2)
 
Introduction to the basics of Python programming (part 3)
Introduction to the basics of Python programming (part 3)Introduction to the basics of Python programming (part 3)
Introduction to the basics of Python programming (part 3)
 
Python Programming Essentials - M34 - List Comprehensions
Python Programming Essentials - M34 - List ComprehensionsPython Programming Essentials - M34 - List Comprehensions
Python Programming Essentials - M34 - List Comprehensions
 
Python - basics
Python - basicsPython - basics
Python - basics
 
PythonIntro
PythonIntroPythonIntro
PythonIntro
 
Python Programming Essentials - M6 - Code Blocks and Indentation
Python Programming Essentials - M6 - Code Blocks and IndentationPython Programming Essentials - M6 - Code Blocks and Indentation
Python Programming Essentials - M6 - Code Blocks and Indentation
 
Python Tutorial
Python TutorialPython Tutorial
Python Tutorial
 
AmI 2015 - Python basics
AmI 2015 - Python basicsAmI 2015 - Python basics
AmI 2015 - Python basics
 
Python教程 / Python tutorial
Python教程 / Python tutorialPython教程 / Python tutorial
Python教程 / Python tutorial
 
Python Workshop
Python WorkshopPython Workshop
Python Workshop
 
python codes
python codespython codes
python codes
 
AmI 2016 - Python basics
AmI 2016 - Python basicsAmI 2016 - Python basics
AmI 2016 - Python basics
 
Blisstering drupal module development ppt v1.2
Blisstering drupal module development ppt v1.2Blisstering drupal module development ppt v1.2
Blisstering drupal module development ppt v1.2
 
EuroPython 2013 - FAST, DOCUMENTED AND RELIABLE JSON BASED WEBSERVICES WITH P...
EuroPython 2013 - FAST, DOCUMENTED AND RELIABLE JSON BASED WEBSERVICES WITH P...EuroPython 2013 - FAST, DOCUMENTED AND RELIABLE JSON BASED WEBSERVICES WITH P...
EuroPython 2013 - FAST, DOCUMENTED AND RELIABLE JSON BASED WEBSERVICES WITH P...
 
FLTK Summer Course - Part I - First Impact - Exercises
FLTK Summer Course - Part I - First Impact - ExercisesFLTK Summer Course - Part I - First Impact - Exercises
FLTK Summer Course - Part I - First Impact - Exercises
 
Advanced Git
Advanced GitAdvanced Git
Advanced Git
 
FLTK Summer Course - Part VIII - Eighth Impact
FLTK Summer Course - Part VIII - Eighth ImpactFLTK Summer Course - Part VIII - Eighth Impact
FLTK Summer Course - Part VIII - Eighth Impact
 
FLTK Summer Course - Part III - Third Impact
FLTK Summer Course - Part III - Third ImpactFLTK Summer Course - Part III - Third Impact
FLTK Summer Course - Part III - Third Impact
 
Manipulating file in Python
Manipulating file in PythonManipulating file in Python
Manipulating file in Python
 

Similaire à Python Tutorial Part 2

Similaire à Python Tutorial Part 2 (20)

PyCourse - Self driving python course
PyCourse - Self driving python coursePyCourse - Self driving python course
PyCourse - Self driving python course
 
20120314 changa-python-workshop
20120314 changa-python-workshop20120314 changa-python-workshop
20120314 changa-python-workshop
 
Modules and Packages in Python Programming Language.pptx
Modules and Packages in Python Programming Language.pptxModules and Packages in Python Programming Language.pptx
Modules and Packages in Python Programming Language.pptx
 
Basics of python
Basics of pythonBasics of python
Basics of python
 
Functions in Python
Functions in PythonFunctions in Python
Functions in Python
 
Introduction to Python and Django
Introduction to Python and DjangoIntroduction to Python and Django
Introduction to Python and Django
 
Python-Yesterday Today Tomorrow(What's new?)
Python-Yesterday Today Tomorrow(What's new?)Python-Yesterday Today Tomorrow(What's new?)
Python-Yesterday Today Tomorrow(What's new?)
 
Interesting Presentation on Python Modules and packages
Interesting Presentation on Python Modules and packagesInteresting Presentation on Python Modules and packages
Interesting Presentation on Python Modules and packages
 
Introduction to Python for Security Professionals
Introduction to Python for Security ProfessionalsIntroduction to Python for Security Professionals
Introduction to Python for Security Professionals
 
Python Programming1.ppt
Python Programming1.pptPython Programming1.ppt
Python Programming1.ppt
 
C- language Lecture 8
C- language Lecture 8C- language Lecture 8
C- language Lecture 8
 
Python Programming for ArcGIS: Part I
Python Programming for ArcGIS: Part IPython Programming for ArcGIS: Part I
Python Programming for ArcGIS: Part I
 
How to write a well-behaved Python command line application
How to write a well-behaved Python command line applicationHow to write a well-behaved Python command line application
How to write a well-behaved Python command line application
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to python
 
Software Programming with Python II.pptx
Software Programming with Python II.pptxSoftware Programming with Python II.pptx
Software Programming with Python II.pptx
 
Python programming language introduction unit
Python programming language introduction unitPython programming language introduction unit
Python programming language introduction unit
 
web programming UNIT VIII python by Bhavsingh Maloth
web programming UNIT VIII python by Bhavsingh Malothweb programming UNIT VIII python by Bhavsingh Maloth
web programming UNIT VIII python by Bhavsingh Maloth
 
Python Foundation – A programmer's introduction to Python concepts & style
Python Foundation – A programmer's introduction to Python concepts & stylePython Foundation – A programmer's introduction to Python concepts & style
Python Foundation – A programmer's introduction to Python concepts & style
 
Introduction to Python Programming
Introduction to Python ProgrammingIntroduction to Python Programming
Introduction to Python Programming
 
Python for Delphi Developers - Part 2
Python for Delphi Developers - Part 2Python for Delphi Developers - Part 2
Python for Delphi Developers - Part 2
 

Plus de Haitham El-Ghareeb

Lecture 9 - DSA - Python Data Structures
Lecture 9 - DSA - Python Data StructuresLecture 9 - DSA - Python Data Structures
Lecture 9 - DSA - Python Data Structures
Haitham El-Ghareeb
 
Data Structures - Lecture 8 - Study Notes
Data Structures - Lecture 8 - Study NotesData Structures - Lecture 8 - Study Notes
Data Structures - Lecture 8 - Study Notes
Haitham El-Ghareeb
 
Lecture 07 Data Structures - Basic Sorting
Lecture 07 Data Structures - Basic SortingLecture 07 Data Structures - Basic Sorting
Lecture 07 Data Structures - Basic Sorting
Haitham El-Ghareeb
 
Research Methodologies - Lecture 02
Research Methodologies - Lecture 02Research Methodologies - Lecture 02
Research Methodologies - Lecture 02
Haitham El-Ghareeb
 

Plus de Haitham El-Ghareeb (20)

مختصر وحدة التعلم الذاتي 2015
مختصر وحدة التعلم الذاتي 2015مختصر وحدة التعلم الذاتي 2015
مختصر وحدة التعلم الذاتي 2015
 
وحدة التعلم الذاتي 2015
وحدة التعلم الذاتي 2015وحدة التعلم الذاتي 2015
وحدة التعلم الذاتي 2015
 
NoSQL Databases, Not just a Buzzword
NoSQL Databases, Not just a Buzzword NoSQL Databases, Not just a Buzzword
NoSQL Databases, Not just a Buzzword
 
EMC Academic Alliance Presentation
EMC Academic Alliance PresentationEMC Academic Alliance Presentation
EMC Academic Alliance Presentation
 
DSA - 2012 - Conclusion
DSA - 2012 - ConclusionDSA - 2012 - Conclusion
DSA - 2012 - Conclusion
 
Lecture 9 - DSA - Python Data Structures
Lecture 9 - DSA - Python Data StructuresLecture 9 - DSA - Python Data Structures
Lecture 9 - DSA - Python Data Structures
 
Data Structures - Lecture 8 - Study Notes
Data Structures - Lecture 8 - Study NotesData Structures - Lecture 8 - Study Notes
Data Structures - Lecture 8 - Study Notes
 
Lect07
Lect07Lect07
Lect07
 
Lecture 07 Data Structures - Basic Sorting
Lecture 07 Data Structures - Basic SortingLecture 07 Data Structures - Basic Sorting
Lecture 07 Data Structures - Basic Sorting
 
LectureNotes-06-DSA
LectureNotes-06-DSALectureNotes-06-DSA
LectureNotes-06-DSA
 
LectureNotes-05-DSA
LectureNotes-05-DSALectureNotes-05-DSA
LectureNotes-05-DSA
 
LectureNotes-04-DSA
LectureNotes-04-DSALectureNotes-04-DSA
LectureNotes-04-DSA
 
LectureNotes-03-DSA
LectureNotes-03-DSALectureNotes-03-DSA
LectureNotes-03-DSA
 
LectureNotes-02-DSA
LectureNotes-02-DSALectureNotes-02-DSA
LectureNotes-02-DSA
 
LectureNotes-01-DSA
LectureNotes-01-DSALectureNotes-01-DSA
LectureNotes-01-DSA
 
Lecture-05-DSA
Lecture-05-DSALecture-05-DSA
Lecture-05-DSA
 
Learn Latex
Learn LatexLearn Latex
Learn Latex
 
Research Methodologies - Lecture 02
Research Methodologies - Lecture 02Research Methodologies - Lecture 02
Research Methodologies - Lecture 02
 
DSA-Lecture-05
DSA-Lecture-05DSA-Lecture-05
DSA-Lecture-05
 
DSA - Lecture 04
DSA - Lecture 04DSA - Lecture 04
DSA - Lecture 04
 

Dernier

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Dernier (20)

Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 

Python Tutorial Part 2

  • 1. Python Tutorial Haitham El-Ghareeb, Ph.D. May, 2012 Twitter: @helghareeb
  • 2. Modules • A module is a file containing Python definitions and statements. • The file name is the module name with the suffix .py appended. • Within a module, the module’s name (as a string) is available as the value of the global variable __name__.
  • 3.
  • 4.
  • 6.
  • 7. Compiled Python Files • As an important speed-up of the start-up time for short programs that use a lot of standard modules, • if a file called spam.pyc exists in the directory where spam.py is found, this is assumed to contain an already-“byte-compiled” version of the module spam. • The modification time of the version of spam.py used to create spam.pyc is recorded in spam.pyc, and the .pyc file is ignored if these don’t match.
  • 8. Compiled Python Files • Normally, you don’t need to do anything to create the spam.pyc file. • Whenever spam.py is successfully compiled, an attempt is made to write the compiled version to spam.pyc. • It is not an error if this attempt fails; if for any reason the file is not written completely, the resulting spam.pyc file will be recognized as invalid and thus ignored later. • The contents of the spam.pyc file are platform independent, so a Python module directory can be shared by machines of different architectures.
  • 9. Some Tips for Experts • When the Python interpreter is invoked with the -O flag, optimized code is generated and stored in .pyo files. • The optimizer currently doesn’t help much; it only removes assert statements. • When -O is used, all bytecode is optimized; .pyc files are ignored and .py files are compiled to optimized bytecode.
  • 10. Some Tips for Experts • Passing two -O flags to the Python interpreter (- OO) will cause the bytecode compiler to perform optimizations that could in some rare cases result in malfunctioning programs. • Currently only __doc__ strings are removed from the bytecode, resulting in more compact .pyo files. • Since some programs may rely on having these available, you should only use this option if you know what you’re doing.
  • 11. Some Tips for Experts • A program doesn’t run any faster when it is read from a .pyc or .pyo file than when it is read from a .py file; the only thing that’s faster about .pyc or .pyo files is the speed with which they are loaded.
  • 12. Some Tips for Experts • When a script is run by giving its name on the command line, the bytecode for the script is never written to a .pyc or .pyo file. • Thus, the startup time of a script may be reduced by moving most of its code to a module and having a small bootstrap script that imports that module. It is also possible to name a .pyc or .pyo file directly on the command line.
  • 13. Some Tips for Experts • It is possible to have a file called spam.pyc (or spam.pyo when -O is used) without a file spam.py for the same module. • This can be used to distribute a library of Python code in a form that is moderately hard to reverse engineer.
  • 14. Some Tips for Experts • The module compileall can create .pyc files (or .pyo files when -O is used) for all modules in a directory.
  • 16. Dir() • The built-in function dir() is used to find out which names a module defines. It returns a sorted list of strings.
  • 17. Packages • Packages are a way of structuring Python’s module namespace by using “dotted module names”. • For example, the module name A.B designates a submodule named B in a package named A. • Just like the use of modules saves the authors of different modules from having to worry about each other’s global variable names, the use of dotted module names saves the authors of multi-module packages like NumPy or the Python Imaging Library from having to worry about each other’s module names.
  • 18.
  • 19. Input and Output • There are several ways to present the output of a program; data can be printed in a human- readable form, or written to a file for future use. This chapter will discuss some of the possibilities. • How do you convert values to strings?
  • 20. Representation • The str() function is meant to return representations of values which are fairly human-readable. • repr() is meant to generate representations which can be read by the interpreter (or will force a SyntaxError if there is not equivalent syntax).
  • 24. I don’t think so! MORE ON STRING FORMATTING?
  • 25. Reading and Writing Files • Open() returns a file object
  • 29. Close
  • 31. Errors and Exceptions • Syntax Errors
  • 45.
  • 48. Make your Class Iterable

Notes de l'éditeur

  1. This does not enter the names of the functions defined in fibo directly in the current symbol table; it only enters the module name fibo there. Using the module name you can access the functions
  2. This imports all names except those beginning with an underscore (_).Note that in general the practice of importing * from a module or package is frowned upon, since it often causes poorly readable code. However, it is okay to use it to save typing in interactive sessions.
  3. Without arguments, dir() lists the names you have defined currently
  4. The __init__.py files are required to make Python treat the directories as containing packages
  5. Luckily, Python has ways to convert any value to a string: pass it to the repr() or str() functions.