From 3b8474a7bf2202d80bb97560dcaa2c8362b3353f Mon Sep 17 00:00:00 2001 From: Mark Pilgrim Date: Fri, 15 May 2009 13:27:35 -0400 Subject: [PATCH] STATUS=CLOSED dropped how-Python-datatypes-compare section and table because it's misleading and I don't care enough to fix it --- dip2 | 39 +++++++--------------------------- your-first-python-program.html | 26 ++--------------------- 2 files changed, 10 insertions(+), 55 deletions(-) diff --git a/dip2 b/dip2 index 40f0ffd..1802b20 100644 --- a/dip2 +++ b/dip2 @@ -806,40 +806,17 @@ buildConnectionString Build a connection string from a dictionary Retur buildConnectionString Build a connection string from a dictionary Returns string. -

4.2. Using Optional and Named Arguments

-

Python allows function arguments to have default values; if the function is called without the argument, the argument gets its default - value. Futhermore, arguments can be specified in any order by using named arguments. Stored procedures in SQL Server Transact/SQL can do this, so if you're a SQL Server scripting guru, you can skim this part. -

Here is an example of info, a function with two optional arguments:


-def info(object, spacing=10, collapse=1):

spacing and collapse are optional, because they have default values defined. object is required, because it has no default value. If info is called with only one argument, spacing defaults to 10 and collapse defaults to 1. If info is called with two arguments, collapse still defaults to 1. -

Say you want to specify a value for collapse but want to accept the default value for spacing. In most languages, you would be out of luck, because you would need to call the function with three arguments. But in -Python, arguments can be specified by name, in any order. -

Example 4.4. Valid Calls of info


-info(odbchelper)  
-info(odbchelper, 12)                
-info(odbchelper, collapse=0)        
-info(spacing=15, object=odbchelper) 
-
    -
  1. With only one argument, spacing gets its default value of 10 and collapse gets its default value of 1. -
  2. With two arguments, collapse gets its default value of 1. -
  3. Here you are naming the collapse argument explicitly and specifying its value. spacing still gets its default value of 10. -
  4. Even required arguments (like object, which has no default value) can be named, and named arguments can appear in any order. -

    This looks totally whacked until you realize that arguments are simply a dictionary. The “normal” method of calling functions without argument names is actually just a shorthand where Python matches up the values with the argument names in the order they're specified in the function declaration. And most of the -time, you'll call functions the “normal” way, but you always have the additional flexibility if you need it. - -
    NoteThe only thing you need to do to call a function is specify a value (somehow) for each required argument; the manner and order - in which you do that is up to you. -
    -

    Further Reading on Optional Arguments

    - -

    4.3. Using type, str, dir, and Other Built-In Functions

    -

    Python has a small set of extremely useful built-in functions. All other functions are partitioned off into modules. This was - actually a conscious design decision, to keep the core language from getting bloated like other scripting languages (cough - cough, Visual Basic). + + +(optional and named arguments stuff was here) + + + + +

    4.3.1. The type Function

    The type function returns the datatype of any arbitrary object. The possible types are listed in the types module. This is useful for helper functions that can handle several types of data.

    Example 4.5. Introducing type

    >>> type(1)           
    diff --git a/your-first-python-program.html b/your-first-python-program.html
    index 316db20..ac75f46 100644
    --- a/your-first-python-program.html
    +++ b/your-first-python-program.html
    @@ -70,33 +70,11 @@ if __name__ == "__main__":
     

    In some languages, functions (that return a value) start with function, and subroutines (that do not return a value) start with sub. There are no subroutines in Python. Everything is a function, all functions return a value (even if it’s None), and all functions start with def.

    -

    The approximate_size function takes the two arguments — size and a_kilobyte_is_1024_bytes — but neither argument specifies a datatype. (As you might guess from the =True syntax, the second argument is a boolean. You’ll learn what that syntax does in [FIXME xref-was-#apihelper].) In Python, variables are never explicitly typed. Python figures out what type a variable is and keeps track of it internally. +

    The approximate_size function takes the two arguments — size and a_kilobyte_is_1024_bytes — but neither argument specifies a datatype. In Python, variables are never explicitly typed. Python figures out what type a variable is and keeps track of it internally.

    In Java and other statically-typed languages, you must specify the datatype of the function return value and each function argument. In Python, you never explicitly specify the datatype of anything. Based on what value you assign, Python keeps track of the datatype internally.

    -

    How Python’s Datatypes Compare to Other Programming Languages

    -

    An erudite reader sent me this explanation of how Python compares to other programming languages: -

    -
    statically typed language
    -
    A language in which types are fixed at compile time. Most statically typed languages enforce this by requiring you to declare all variables with their datatypes before using them. Java and C are statically typed languages. -
    -
    dynamically typed language
    -
    A language in which types are discovered at execution time; the opposite of statically typed. JavaScript and Python are dynamically typed, because they figure out what type a variable is when you first assign it a value. -
    -
    strongly typed language
    -
    A language in which types are always enforced. Java and Python are strongly typed. If you have an integer, you can’t treat it like a string without explicitly converting it. -
    -
    weakly typed language
    -
    A language in which types are “automagically” coerced to other types as needed; the opposite of strongly typed. PHP is weakly typed. In PHP, you can concatenate the string '12' and the integer 3 to get the string '123', then treat that as the integer 123, all without any explicit conversion. -
    -
    -

    So Python is both dynamically typed (because it doesn’t use explicit datatype declarations) and strongly typed (because once a variable has a datatype, it actually matters). -

    If you have experience in other programming languages, this table may help you visualize how Python compares to them: - -
    Statically typedDynamically typed -
    Weakly typedC, Objective-CJavaScript, Perl 5, PHP -
    Strongly typedPascal, JavaPython, Ruby -
    +

    Writing Readable Code

    I won’t bore you with a long finger-wagging speech about the importance of documenting your code. Just know that code is written once but read many times, and the most important audience for your code is yourself, six months after writing it (i.e. after you’ve forgotten everything but need to fix something). Python makes it easy to write readable code, so take advantage of it. You’ll thank me in six months.

    Documentation Strings