Python: Difference between revisions

From Elvanör's Technical Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
== Important changes from Java ==
= Important changes from Java =


* When you pass an object to a function, and "reassign" it using its local function argument name, the outside objects won't get reassigned. This is because Python reassigns the local name only. This is a important difference from Java.
* When you pass an object to a function, and "reassign" it using its local function argument name, the outside objects won't get reassigned. This is because Python reassigns the local name only. This is a important difference from Java.


== Useful techniques ==
= Useful techniques =


* To iterate over two lists at the same time, use the zip built-in function:
* To iterate over two lists at the same time, use the zip built-in function:
Line 9: Line 9:
  for a,b in zip(list1, list2):
  for a,b in zip(list1, list2):


== Casts ==
= Casts =


* To cast a float to an integer, you can use the built-in int() function.
* To cast a float to an integer, you can use the built-in int() function.


== Exceptions ==
= Exceptions =


* Sample code to deal with an exception:
* Sample code to deal with an exception:
Line 27: Line 27:
This will give you information about the raised exception type.
This will give you information about the raised exception type.


== Scopes ==
= Scopes =


Within a module, inside a function, you can access the module variables normally. However, assigning them is not possible (you would assign a local variable). To assign a module variable inside a function, you need to specify that the variable is global by using the global keyword.
Within a module, inside a function, you can access the module variables normally. However, assigning them is not possible (you would assign a local variable). To assign a module variable inside a function, you need to specify that the variable is global by using the global keyword.
Line 35: Line 35:
This is quite strange!
This is quite strange!


== Classes ==
= Classes =


* You don't need to explicitely declare class fields, as they are dynamically created the first time you assign them.
* You don't need to explicitely declare class fields, as they are dynamically created the first time you assign them.
* Every class method should take as a first argument "self".
* Every class method should take as a first argument "self".
* Accessing an object attribute, when the attribute does not exist, results in an exception. Code like:
if object.myAttribute:
will not work as expected if myAttribute was not assigned. One easy solution is to define in the class initializer method (__init__) the attribute and set it equal to None.

Revision as of 19:06, 1 September 2008

Important changes from Java

  • When you pass an object to a function, and "reassign" it using its local function argument name, the outside objects won't get reassigned. This is because Python reassigns the local name only. This is a important difference from Java.

Useful techniques

  • To iterate over two lists at the same time, use the zip built-in function:
for a,b in zip(list1, list2):

Casts

  • To cast a float to an integer, you can use the built-in int() function.

Exceptions

  • Sample code to deal with an exception:
	try:
	    doSomething()
	except Exception, inst:
	    print str(inst.args)
	    print str(sys.exc_info()[0])

This will give you information about the raised exception type.

Scopes

Within a module, inside a function, you can access the module variables normally. However, assigning them is not possible (you would assign a local variable). To assign a module variable inside a function, you need to specify that the variable is global by using the global keyword.

global myVariable

This is quite strange!

Classes

  • You don't need to explicitely declare class fields, as they are dynamically created the first time you assign them.
  • Every class method should take as a first argument "self".
  • Accessing an object attribute, when the attribute does not exist, results in an exception. Code like:
if object.myAttribute:

will not work as expected if myAttribute was not assigned. One easy solution is to define in the class initializer method (__init__) the attribute and set it equal to None.