SlideShare ist ein Scribd-Unternehmen logo
1 von 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!

Weitere ähnliche Inhalte

Was ist angesagt?

Python quick guide1
Python quick guide1Python quick guide1
Python quick guide1
Kanchilug
 

Was ist angesagt? (20)

Introduction to python
Introduction to pythonIntroduction to python
Introduction to python
 
Fundamentals of Python Programming
Fundamentals of Python ProgrammingFundamentals of Python Programming
Fundamentals of Python Programming
 
Python
PythonPython
Python
 
Python ppt
Python pptPython ppt
Python ppt
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to python
 
Python Programming ppt
Python Programming pptPython Programming ppt
Python Programming ppt
 
Python programming introduction
Python programming introductionPython programming introduction
Python programming introduction
 
Python introduction
Python introductionPython introduction
Python introduction
 
Python programming
Python programmingPython programming
Python programming
 
Variables in python
Variables in pythonVariables in python
Variables in python
 
Beginning Python Programming
Beginning Python ProgrammingBeginning Python Programming
Beginning Python Programming
 
Variables & Data Types In Python | Edureka
Variables & Data Types In Python | EdurekaVariables & Data Types In Python | Edureka
Variables & Data Types In Python | Edureka
 
Python Presentation
Python PresentationPython Presentation
Python Presentation
 
Python - An Introduction
Python - An IntroductionPython - An Introduction
Python - An Introduction
 
Python quick guide1
Python quick guide1Python quick guide1
Python quick guide1
 
Python and its Applications
Python and its ApplicationsPython and its Applications
Python and its Applications
 
Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)
 
Intro to Python
Intro to PythonIntro to Python
Intro to Python
 
Python programming
Python  programmingPython  programming
Python programming
 
Python Basics | Python Tutorial | Edureka
Python Basics | Python Tutorial | EdurekaPython Basics | Python Tutorial | Edureka
Python Basics | Python Tutorial | Edureka
 

Andere mochten auch

PythonIntro
PythonIntroPythonIntro
PythonIntro
webuploader
 

Andere mochten auch (20)

Python Tutorial Part 1
Python Tutorial Part 1Python Tutorial Part 1
Python Tutorial Part 1
 
Python tutorial
Python tutorialPython tutorial
Python tutorial
 
Introduction to the basics of Python programming (part 1)
Introduction to the basics of Python programming (part 1)Introduction to the basics of Python programming (part 1)
Introduction to the basics of Python programming (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 Basics
Python BasicsPython Basics
Python Basics
 
python codes
python codespython codes
python codes
 
Python basics
Python basicsPython basics
Python basics
 
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
 

Ähnlich wie Python Tutorial Part 2

Ähnlich wie Python Tutorial Part 2 (20)

PyCourse - Self driving python course
PyCourse - Self driving python coursePyCourse - Self driving python course
PyCourse - Self driving python course
 
Python libraries
Python librariesPython libraries
Python libraries
 
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
 

Mehr von 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
 

Mehr von 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
 

KĂźrzlich hochgeladen

+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@
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 

KĂźrzlich hochgeladen (20)

GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.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...
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
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...
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
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
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
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
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
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)
 

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
  • 45.
  • 48. Make your Class Iterable

Hinweis der Redaktion

  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.