Python高一点基础
不回说一点话,简简单单粘贴点代码,一是慰藉,二是寻找知识的历史轨迹~~~
Python的class声明
def outSide():
print("outside method")
class MyClass:
sharedVar = "testVariable"
f = outSide
def __init__(self, args1: object, args2: object) -> object:
self.ar1 = args1
self.ar2 = args2
def test(self):
return "test class method"
if __name__ == "__main__":
xx = MyClass("jjj", "babb")
print(xx.ar1)
print(xx.ar2)
print(xx.sharedVar)
test1 = xx.test
print(test1())
xxx = MyClass("jjj", "test")
print(MyClass.f)
Python的异常
class B(Exception):
pass
class C(B):
pass
class D(C):
pass
class DemoException(Exception):
def __init__(self, x: object, y: object):
self.xx = x
self.yy = y
if __name__ == "__main__":
for cls in [B, C, D]:
try:
print("111111")
raise cls()
print("232323")
except D:
print("exception D")
except C:
print("exception C")
except B:
print("exception B")
try:
i = int(input("please input number:"))
print("input i:", i)
except Exception:
print("oops input data is not number, try again")
finally:
print("finally")
try:
x = 4
y = 3
if x % y > 0:
print("x:", x, "y:", y)
raise DemoException(x, y)
print("23232323233")
except DemoException as demo:
print(demo.xx / demo.yy)
break
Python 的线程
import threading
class DemoThread(threading.Thread):
def __init__(self, array1):
threading.Thread.__init__(self)
self.arr = array1
def run(self):
for i in self.arr:
print("i", i)
array = ["11", "232", "3232344", "888"]
thread = DemoThread(array)
thread.start()
Python 的作用域
def scope_test():
def do_local():
spam = "local spam"
def do_nonlocal():
nonlocal spam
spam = "nonlocal spam"
def do_global():
global spam
spam = "global spam"
spam = "test spam"
do_local()
print("After local assignment :", spam)
do_nonlocal()
print("After nonlocal assignment :", spam)
do_global()
print("After global assignment :", spam)
if __name__ == "__main__":
scope_test()
print("After local assignment :", spam)