Last active
August 29, 2015 14:01
-
-
Save icsaas/93a7aed7c09fac68ac81 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def foo(bar=[]): | |
bar.append("baz") | |
return bar |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def foo(bar=None): | |
if bar=None: | |
bar=[] | |
bar.append("baz"0 | |
return bar | |
class A(object): | |
x=1 | |
class B(A): | |
pass | |
class C(A): | |
pass | |
>>>print A.x,B.x,C.x | |
1 1 1 | |
>>>B.x=2 | |
>>>print A.x,B.x,C.x | |
1 2 1 | |
>>>A.x=3 | |
>>>print A.x,B.x,C.x | |
3 2 3 | |
try: | |
l=["a","b"] | |
except (ValueError,IndexError) as e: | |
pass | |
def create_multipliers(): | |
return [lambda x:i*x for i in range(5)] | |
for multiplier in create_multipliers(): | |
print multiplier(2) | |
def create_multipliers(): | |
return [lambda x,i=i:i*x for i in range(5)] | |
for multiplier in create_multipliers(): | |
print multiplier(2) | |
#python2 python3 friendly compatiable | |
import sys | |
def bar(i): | |
if i==1: | |
raise KeyError(1) | |
if i==2: | |
raise ValueError(2) | |
def good(): | |
exception = None | |
try: | |
bar(int(sys.argv[1])) | |
except KeyError as e: | |
exception =e | |
print('Key error') | |
except ValueError as e: | |
exception = e | |
print('value error') | |
print(exception) | |
good() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment