今さらPython3 (26) - 関数を作る

ようやく関数を作るところまで来ました。(第4章)

入門 Python 3

入門 Python 3

defで定義することぐらい知ってる

>>> def do_nothing:
  File "<stdin>", line 1
    def do_nothing:
                  ^
SyntaxError: invalid syntax

引数なくても、()付けなきゃ行けないのに忘れるパターンで怒られた。仕切り直し。

>>> def do_nothing():
...     pass
... 
>>> do_nothing()

何かを出力する関数にしたのが下のヤツ。

>>> def make_a_sound():
...     print('quack')
... 
>>> make_a_sound()
quack

戻り値を渡してみよう。

>>> def agree():
...     return True
... 
>>> if agree():
...     print('splendid!')
... else:
...     print('That was unexpected.')
... 
splendid!
>>> x = agree()
>>> x
True
>>> 

引数を使って見るなら、こう。

>>> def echo(anything):
...     return anything + ' ' + anything
... 
>>> echo('Rumplestiltskin')
'Rumplestiltskin Rumplestiltskin'
>>> echo('foo')
'foo foo'
>>> 

条件分岐と組み合わせて。

>>> def commentary(color):
...     if color == 'red':
...         return "It's a tomato."
...     elif color == 'green':
...         return "It's a green pepper."
...     elif color == 'bee purple':
...         return "I don't know what it is, but only bees can see it."
...     else:
...         return "I've never heard of the color " color "."
  File "<stdin>", line 9
    return "I've never heard of the color " color "."
                                                ^
SyntaxError: invalid syntax
>>> "I've never heard of the color " "abac" "."
"I've never heard of the color abac."
>>> color = 'abac'
>>> "I've never heard of the color " color "."
  File "<stdin>", line 1
    "I've never heard of the color " color "."
                                         ^
SyntaxError: invalid syntax
>>>
>>> def commentary(color):
...     if color == 'red':
...         return "It's a tomato."
...     elif color == 'green':
...         return "It's a green pepper."
...     elif color == 'bee purple':
...         return "I don't know what it is, but only bees can see it."
...     else:
...         return "I've never heard of the color " + color + "."
... 
>>> commentary('red')
"It's a tomato."
>>> commentary('green')
"It's a green pepper."
>>> commentary('bee purple')
"I don't know what it is, but only bees can see it."
>>> commentary('black')
"I've never heard of the color black."

テキスト通りにやったら、syntax errorが出たので少しだけアレンジ。returnの中で変数を使ったら怒られたみたい。代わりに+を使って1つに繋げて出してみた。

>>> print(do_nothing())
None

戻り値がない関数の場合は、一応Noneを返すみたい。

Noneは役立つとおっしゃっているので

囲みの中もやってみよう。

>>> thing = None
>>> if thing:
...    print("It's some thing")
... else:
...    print("It's no thing")
... 
It's no thing
>>> 
>>> if thing is None:
...     print("It's nothing")
... else:
...     print("It's something")
... 
It's nothing

上の方は、Trueではないという意味でFalseと同じように扱われているけど、下の方はNoneと明確に条件分岐に書かれている。空っぽと無を区別していると。なんか禅問答みたいw

>>> def is_none(thing):
...     if thing is None:
...         print("It's None")
...     elif thing:
...         print("It's True")
...     else:
...         print("It's False")
... 
>>> is_none(None)
It's None
>>> is_none(True)
It's True
>>> is_none(False)
It's False
>>> is_none(0)
It's False
>>> is_none(0.0)
It's False
>>> is_none(())
It's False
>>> is_none([])
It's False
>>> is_none({})
It's False
>>> is_none(set())
It's False

前も似たような事をやったけど、こんな感じでNoneとFalseが違うと言う事が分かるのね。

(つづく)