LoginSignup
0
0

More than 5 years have passed since last update.

[python]関数オブジェクト(?)の値

Posted at

まとめ

pythonで関数オブジェクト(っていうのかわかりませんが...)の値はTrueです.
2.7で確認しました.

def func():
    return False

if func:
    print('true')
else:
    print('false')

# true

if func():
    print('true')
else:
    print('false')

# false

ハマったパターン

class Test(object):
    def __init__(self):
        self._flag = False

    # ここで@propertyを付けたつもりが付け忘れていた
    def flag(self):
        print('test flag')
        return self._flag

# ---

if __name__ == '__main__':
    test = Test()

    # propertyにしてると思い込んでるからカッコ無しで呼び出す
    if test.flag:
        print('true')
    else:
        print('false')

# 'test flag'はprintされず,
# 'true'だけがprintされる

しょーもないミスですが,あるんじゃないでしょうか?
いや,自分がショボすぎるだけか...

0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0