-
Notifications
You must be signed in to change notification settings - Fork 0
[類別篇] 方法
Tsung-Jung Tsai (TJ_Tsai) edited this page Nov 24, 2021
·
5 revisions
#參數 #parameters #註釋 #annotation #說明
from typing import List, Union
def show_messages(messages: Union[str, List[str]] ):
if isinstance(messages, str):
print(messages)
elif isinstance(messages, List):
for m in messages:
print('-', m)
else:
print(f'type({messages})={type(messages)}: unsupported')
show_messages('hi')
show_messages(['hi', 'hello'])
show_messages(123)
執行結果:
hi
- hi
- hello
type(123)=<class 'int'>: unsupported
class Add:
def execute(self, x, y):
return x + y
def __call__(self, x, y):
return x + y
x = 1
y = 2
add = Add()
print("add.execute({x}, {y}) = {z}".format(x=x, y=y, z=add.execute(x, y)))
print("add({x}, {y}) = {z}".format(x=x, y=y, z=add.execute(x, y)))
執行結果:
add.execute(1, 2) = 3
add(1, 2) = 3
tj_tsai / tsungjung411@yahoo.com.tw / tsungjung411@gmail.com