Feel free to contribute as you see fit.
_
In the interactive python shell, the special variable _ is equivalent to Ans in TI-Basic. It holds the last returned value.
classmethod and staticmethod
Most object oriented Python code doesn't make use of these two, but they're very useful for declaring static functions inside a class. Normally if you have a class definition like so:
Code:
and the interpreter automatically passes blahobj as the self argument to blahblah. Sometimes; however, you want a Java + C style static functions that belong to the whole class, not just to a single instance (generally utility functions related to the purpose of the class). In this case you can declare a function to be a classmethod, which receives the class object it is called on as the implicit first argument; or with staticmethod, which receives no implicit first argument at all. Static methods should be used when the function will always be called on the same class. Class methods should be used when the function will be called on different classes with different implementations of the same function. To declare a class or static method, you have two options. Decorator syntax:
Code:
or the traditional way:
Code:
unpacking + zip
say you want to return multiple values from a function.
Code:
min = 1
median = 6
max = 9
This works for tuples and lists.
if for some strange reason you want one-tuples, you can create them like this:
Code:
and to unpack the one-tuple
Code:
You can also unpack lists, tuples, and dictionaries into a function call.
Code:
to pack an arbitrary number of arguments into a list you can do the reverse.
Code:
The same works for dictionaries and keyword arguments. It's hard to think of trivial examples for keyword arguments, so for this I'm just gonna link you to the python tutorial. http://www.network-theory.co.uk/docs/pytut/KeywordArguments.html
Because python is an interpreted language, you can actually dump your programs configuration settings to a text file, and then use the eval() function to read it back later. This should be used with caution, since eval will execute any valid python code in the input.
One last thing: zip(). It will take its arguments and zip them together. The easiest way to explain this is to see it in action.
Code:
At this point, zlist equals [(1, 4), (2, 5), (3, 6)]. To unzip this back into it's constituent parts, you can do this:
Code:
l2 now equals [1, 2, 3] and l1 now equals [4, 5, 6]
list comprehensions
basically, a one-line way of building a list from some input.
for example, say you want a list of the ascii values of every character in a string.
Code:
output would now equal [79, 109, 103, 104, 97, 105, 103, 117, 121, 115]
You can also conditionally include parts of the list. For example, to remove all the vowels from a string you could do this.
Code:
output would now equal "mghgys"
magic slicing
Most of you should be aware of the ability to slice to the end of a list, or from the beginning of a list (e.g. list[:5] is equivalent to list[0:5] and list[5:] is equivalent to list[5:len(list)]), but there are a couple additional features that are nice. you can use a negative list index to get the last n items in a list. For example list[-5:] will retrieve the last 5 items in a list, and list[:-5] will retrieve all but the last 5. Better still is slicing with 2 colons. list[2::3] will retrieve every 3rd item in list, starting from an index of 2. For example
Code:
will return every multiple of 3 between 0 (inclusive) and 100 (exclusive), or to use mathematical notation all multiples of 3 in [0, 100).
_
In the interactive python shell, the special variable _ is equivalent to Ans in TI-Basic. It holds the last returned value.
classmethod and staticmethod
Most object oriented Python code doesn't make use of these two, but they're very useful for declaring static functions inside a class. Normally if you have a class definition like so:
Code:
class Blah:
def blahblah(self, other, arguments):
dosomething()
blahobj = Blah()
blahobj.blahblah(42, 1337 )
and the interpreter automatically passes blahobj as the self argument to blahblah. Sometimes; however, you want a Java + C style static functions that belong to the whole class, not just to a single instance (generally utility functions related to the purpose of the class). In this case you can declare a function to be a classmethod, which receives the class object it is called on as the implicit first argument; or with staticmethod, which receives no implicit first argument at all. Static methods should be used when the function will always be called on the same class. Class methods should be used when the function will be called on different classes with different implementations of the same function. To declare a class or static method, you have two options. Decorator syntax:
Code:
class Blah:
@staticmethod
def staticMethod1():
print "staticMethod1"
@classmethod
def classMethod1(cls):
print "classMethod1 called on" + str(cls)
or the traditional way:
Code:
class Blah:
def staticMethod2():
print "staticMethod2"
staticMethod2 = staticmethod(staticMethod2)
def classMethod2(cls):
print "classMethod2 called on" + str(cls)
classMethod2 = classmethod(classMethod2)
unpacking + zip
say you want to return multiple values from a function.
Code:
def getMinMedianMax(list):
return (min(list), max(list), sorted(list)[len(list) / 2])
list = (1, 3, 6, 6, 9, 2)
min, median, max = getMinMedianMax(list)
min = 1
median = 6
max = 9
This works for tuples and lists.
if for some strange reason you want one-tuples, you can create them like this:
Code:
onetuple = (1, )
and to unpack the one-tuple
Code:
firstval, = onetuple
You can also unpack lists, tuples, and dictionaries into a function call.
Code:
def sumTwoValues(one, two):
return one + two
list = [1, 2]
sum = sumTwoValues(*list)
to pack an arbitrary number of arguments into a list you can do the reverse.
Code:
def sumArgs(*args):
return sum(args)
sum = sumArgs(1, 2, 3)
The same works for dictionaries and keyword arguments. It's hard to think of trivial examples for keyword arguments, so for this I'm just gonna link you to the python tutorial. http://www.network-theory.co.uk/docs/pytut/KeywordArguments.html
Because python is an interpreted language, you can actually dump your programs configuration settings to a text file, and then use the eval() function to read it back later. This should be used with caution, since eval will execute any valid python code in the input.
One last thing: zip(). It will take its arguments and zip them together. The easiest way to explain this is to see it in action.
Code:
l1 = [1, 2, 3]
l2 = [4, 5, 6]
zlist = zip(l1, l2)
At this point, zlist equals [(1, 4), (2, 5), (3, 6)]. To unzip this back into it's constituent parts, you can do this:
Code:
l2, l1 = zip(*zlist)
l2 now equals [1, 2, 3] and l1 now equals [4, 5, 6]
list comprehensions
basically, a one-line way of building a list from some input.
for example, say you want a list of the ascii values of every character in a string.
Code:
input = "Omghaiguys"
output = [ord(letter) for letter in input]
output would now equal [79, 109, 103, 104, 97, 105, 103, 117, 121, 115]
You can also conditionally include parts of the list. For example, to remove all the vowels from a string you could do this.
Code:
input = "Omghaiguys"
output = "".join([letter for letter in input if letter.lower() not in "aeiou"])
output would now equal "mghgys"
magic slicing
Most of you should be aware of the ability to slice to the end of a list, or from the beginning of a list (e.g. list[:5] is equivalent to list[0:5] and list[5:] is equivalent to list[5:len(list)]), but there are a couple additional features that are nice. you can use a negative list index to get the last n items in a list. For example list[-5:] will retrieve the last 5 items in a list, and list[:-5] will retrieve all but the last 5. Better still is slicing with 2 colons. list[2::3] will retrieve every 3rd item in list, starting from an index of 2. For example
Code:
range(100)[2::3]
will return every multiple of 3 between 0 (inclusive) and 100 (exclusive), or to use mathematical notation all multiples of 3 in [0, 100).