Honesty

 

Book_DiveIntoPython

Page history last edited by Honesty 9 mos ago


Book Details

 

Dive Into Python

  • Mark Pilgrim
  • Apress
  • July 19, 2004

Top


Chapter 1. Installing Python

1.2. Python on Windows

  • ActiveState makes a Windows installer for Python called ActivePython, which includes a complete version of Python, an IDE with a Python−aware code editor, plus some Windows extensions for Python that allow complete access to Windows−specific services, APIs, and the Windows Registry.
  • ActivePython is freely downloadable, although it is not open source. It is the IDE I used to learn Python, and I recommend you try it unless you have a specific reason not to.
  • One such reason might be that ActiveState is generally several months behind in updating their ActivePython installer when new version of Python are released.

Top


Chapter 2. Your First Python Program

2.2. Declaring Functions

 

def buildConnectionString(params):

 

  • Python has functions like most other languages, but it does not have separate header files like C++ or interface/implementation sections like Pascal.
  • When you need a function, just declare it.
  • Note that the keyword def starts the function declaration, followed by the function name, followed by the arguments in parentheses. Multiple arguments are separated with commas.
  • Also note that the function doesn't define a return datatype.

    In fact, every Python function returns a value;

    if the function ever executes a return statement, it will return that value, otherwise it will return None, the Python null value.

  • The argument doesn't specify a datatype.

    In Python, variables are never explicitly typed. Python figures out what type a variable is and keeps track of it internally.

  • Python is both:
    • dynamically typed: doesn't use explicit datatype declarations, and
    • strongly typed: once a variable has a datatype, it actually matters

 

2.3. Documenting Functions

 

"""Build a connection string from a dictionary of parameters.

Returns string."""

 

  • Triple quotes signify a multi−line string
  • Everything between the start and end quotes is part of a single string, including carriage returns and other quote characters
  • You can use them anywhere, but you'll see them most often used when defining a doc string
  • Triple quotes are also an easy way to define a string with both single and double quotes
  • Everything between the triple quotes is the function's doc string, which documents what the function does
  • A doc string, if it exists, must be the first thing defined in a function (that is, the first thing after the colon)
  • You don't technically need to give your function a doc string, but you always should.

    Python gives you an added incentive: the doc string is available at runtime as an attribute of the function.

  • Many Python IDEs use the doc string to provide context−sensitive documentation, so that when you type a function name, its doc string appears as a tooltip.
  • more about doc string:

 

2.4. Everything Is an Object

 

import odbchelper

params = {"server":"mpilgrim", "database":"master", "uid":"sa", "pwd":"secret"}

print odbchelper.buildConnectionString(params)

print odbchelper.buildConnectionString.__doc__

 

  • A function, like everything else in Python, is an object
  • The first line imports the odbchelper program as a module −− a chunk of code that you can use interactively, or from a larger Python program.

    Once you import a module, you can reference any of its public functions, classes, or attributes.

    Modules can do this to access functionality in other modules, and you can do it in the IDE too.

  • When you want to use functions defined in imported modules, you need to include the module name.

    Once you import a Python module, you access its functions with module.function

  • Instead of calling the function as you would expect to, you asked for one of the function's attributes, __doc__.

Comments (0)

You don't have permission to comment on this page.