import time ms = int(round(time.time() * 1000)) print(ms)
class A:
@staticmethod
def m():
print "A"
class B(A):
@staticmethod
def m():
print "B"
a = A()
a.m() # A
b = B()
b.m() # B
class A(object): def __init__(self): print "world" class B(A): def __init__(self): print "hello" super(B, self).__init__()
class A: def __init__(self): print "world" class B(A): def __init__(self): print "hello" A.__init__(self)
def equals_ignore_case(s1, s2):
if s1 is None and s2 is None:
return True
if s1 is None and s2 is not None:
return False
if s1 is not None and s2 is None:
return False
return s1.lower() == s2.lower()
def contains(dict, key):
return key in dict
dict={}
dict['a']=1
dict['b']=2
dict['c']=3
print(contains(dict, 'a')) # True
print(contains(dict, 'd')) # False
if e in list:
list.remove(e)
while e in list:
list.remove(e)
try:
list.remove(e)
except ValueError:
# not in the list
pass
while True:
try:
list.remove(e)
except ValueError:
break
Easier to ask for forgiveness than permission. This common Python coding style assumes the existence of valid keys or attributes and catches exceptions if the assumption proves false. This clean and fast style is characterized by the presence of many try and except statements. The technique contrasts with the LBYL style common to many other languages such as C.