Skip to content

[類別篇] 方法

Tsung-Jung Tsai (TJ_Tsai) edited this page Sep 8, 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
Clone this wiki locally