用書 PYTHON王者歸來 作者洪錦魁
type() 查看物件資料類型
class Grandpa():
pass
class Papa(Grandpa):
pass
class son(Papa):
def name(self):
pass
Nick = son()
Mike = Papa()
Amber = Grandpa()
print(type(Nick))
print(type(Nick.name))
print(type(Mike))
print(type(Amber))

執行結果

isintance()傳回物件是否為某一類建的類別
class Grandpa():
pass
class Papa(Grandpa):
pass
class son(Papa):
def name(self):
pass
Nick = son()
Mike = Papa()
Amber = Grandpa()
print(isinstance(Nick,son)) #True
print(isinstance(Nick,Papa)) #True
print(isinstance(Nick,Grandpa))#True
print(isinstance(Mike,son)) #False
print(isinstance(Mike,Papa)) #True
print(isinstance(Mike,Grandpa)) #True
print(isinstance(Amber,son)) #False
print(isinstance(Amber,Papa)) #False
print(isinstance(Amber,Grandpa)) #True

執行結果

特殊屬性
文件字串__doc__
def getMin(x,y):
"Please input the two number.It will return small number"
if int(x)< int(y):
return x
else:
return y
print(getMin(2,5))
print(getMin.__doc__)


class mymath:
"My math"
def getMin(x,y):
"Please input the two number.It will return small number"
if int(x)< int(y):
return x
else:
return y
def getMax(x,y):
"Please input the two number.It will return big number"
if int(x)> int(y):
return x
else:
return y
print(mymath.__doc__)
print(mymath.getMin.__doc__)
print(mymath.getMax.__doc__)
Max = mymath.getMax(5,2)
Min = mymath.getMin(6,4)
print(Max)
print(Min)

執行結果

name() 若上述程式是自己執行 那__name__ 一定是 __main__

執行結果


執行結果

__name__ 代表自己執行或是被其他程式import導入當作模組使用
若是被其他程式import導入使用,__name__ 會變成檔案名稱
_str__()返回易取字串
class memo1():
def __init__(self,name):
self.name = name
Name1 = memo1("Nini")
print(Name1)
class memo2():
def __init__(self,name):
self.name = name
def __str__(self):
return f"{self.name}"
Name2 = memo2("Nini")
print(Name2)

執行結果

__repr__()
上述,如果在python shell 輸入Name2 ,他還是會跑出 Name1 不易閱讀的結果
<main.memo1 object at 0x00000256DDC98FD0>
因為系統是呼叫__repr__()做回應,所以把__repr__()定義成和__str__()相同 __repr__ =__str__
class memo2():
def __init__(self,name):
self.name = name
def __str__(self):
return f"{self.name}"
__repr__ =__str__
Name2 = memo2("Nini")
print(Name2)

__iter__()迭代物件
參考 知乎 不太理解這個的意思……..
class Mymath():
def __init__(self,max):
self.max = max
def __iter__(self):
self.a = 0
self.b = 1
return self
def __next__(self):
mymath = self.a
if mymath >self.max:
raise StopIteration
self.a,self.b =self.b,self.a+self.b
return mymath
for i in Mymath(100):
print(i)

執行結果

__eq__() 字串或其他內容是否相同
class Name1():
def __init__(self,name1):
self.name1 =name1
def eq(self,name2):
return self.name1.upper() == name2.name1.upper()
A =Name1("Nini")
B =Name1("nini")
C =Name1("Nick")
print(A.eq(B))
print(A.eq(C))
class Name2():
def __init__(self,name1):
self.name1 =name1
def __eq__(self,name2):
return self.name1.upper() == name2.name1.upper()
D =Name2("Nini")
E =Name2("nini")
F =Name2("Nick")
print(D==E)
print(D==F)

執行結果

算出圓的面積和邊長
class myroom():
def __init__(self):
self.color ="Pink"
class maru(myroom):
def __init__(self,r):
super().__init__()
self.pi =3.14
self.r = r
def get_r(self):
return self.r
def set_r(self,r):
self.r = r
def get_dimameter(self):
return self.r * 2
def get_perimeter(self):
return self.r *2* self.pi
def get_area(self):
return (self.r **2)* self.pi
def get_color(self):
return color
Maru =maru(5)
print(Maru.color)
print(Maru.get_r())
print(Maru.get_dimameter())
print(Maru.get_perimeter())
print(Maru.get_area())
Maru.set_r(10)
print(Maru.color)
print(Maru.get_r())
print(Maru.get_dimameter())

執行結果

注意
print(Maru.set_r) >>>>沒有辦法印出r 這邊應該是一個物件

print(Maru.color)這邊只能這樣打才能印出字串
print(Maru.get_color) 會印出物件

我覺得這樣改比較淺顯易懂


執行結果
