What's new in Python 3.0

Nikolaus Gradwohl2009-02-09T18:48:00+00:00

i have played a bit with python3.0 and this are the things that catched my eye:

  • the new print syntax
  • the exception-handling
  • decorators
  • the new io module
  • classes in python

are no complete list of changes between 2.5 and 3.0 - and don't want to be. they are just a list of thing's i noticed. Some of them are already present in python 2.6 but i newver used that release - so i don't realy know.

new print syntax

the first thing that every python user will notice is that print now need brackets

print( "Hello World" )

the cool feature of the new print function is, that the ending of the line can be defined by using the named parameter end so to print AB the following code can be used

print( "A", end="" )
print( "B" )

another named parameter that is used to separate values is sep

print( 1, 2, sep="-")

the third very interesting parameter is file. it is used to redirect the output to another file. To print to stderr for example the following code is used

import sys
print( "filetest", file=sys.stderr )

exceptions

catching exception is shown in the next codeblock. The binding of the exception to the variable is done by the as keyword. the variable is defined only in the scope of the except-block

try:
    print( "Body" )

except Exception as e:
    print( "Exception", e )
finally:
    print( "Finally" )

to raise an exception the following syntax is used

raise Exception( "bla ..." )

all exceptions MUST extend BasicException and SHOULD extend Exception

decorators

decorators are syntactic sugar simmilar to annotations in java or attributes in c#. in reality they are just functions that can be used to implement some behavior before or after a function or replace the called function completely. they are often used to define some validation functions

def foo( func ):
    def newfunc( *args, **kwargs ):
        print( "do some stuff before"  )
        func( *args, **kwargs )
        print( "do some stuff after"  )

    return newfunc


@foo
def test():
    print( "I'm the function body" )

test()

io objects

file access is now handled by the new io module. The classes provided by this module define rawIO, FileIO, BufferedIO but also StringIO which can be used to print data to a string buffer.

the io classes define methods like open, close, read, readline, write, etc

the following code prints the contents of text.txt

f = io.open( "text.txt" )
print( f.read() )
f.close()

another way to use files is the with command. in the following example the file is automaticaly closed at the end of the with block

with io.open( "io.py" ) as f:
    print( f.read() )

the following code can be used to print a file with linenumbers

with io.open( "io.py" ) as f:
    l = f.readline()
    i = 1
    while( l ):
        print( "%2d" %i, ":", l[0:-1] )
        l = f.readline()
        i+=1

(i'm sure this could be done in one line, a more pythonic way, more beatuiful, etc, etc - i just wanted to show the use of the readline method :-) )

new classes

all classes are newstyle classes in python3. The following code shows how to define a property and some getters and setters for it

class C(object):
    def __init__(self):
        self.__x = 0

    def getx(self):
        return self.__x

    def setx(self, x):
        if x < 0: x = 0
        self.__x = x

    x = property(getx, setx)
Tweet This! submit to reddit Digg! Tags: | no comments | no trackbacks

See also:

grease pencil experiment 6 - grid
grease pencil experiment 4 - filled shapes
grease pencil experiment 4 - depth of field
grease pencil experiment 3 - animated circles
grease pencil experiment 2 - freestyle

Trackbacks

Comments

Leave a response

Leave a comment