Skip to content

Instantly share code, notes, and snippets.

@ytyng
Last active July 29, 2018 02:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ytyng/49dbfd7ee858147fb1e0bbba9ba59a78 to your computer and use it in GitHub Desktop.
Save ytyng/49dbfd7ee858147fb1e0bbba9ba59a78 to your computer and use it in GitHub Desktop.
class MyClass:
@property
def always_true(self):
print('in_always_true')
return True
@property
def always_false(self):
print('in_always_false')
return False
@property
def p_hoge(self):
print('in_p_hoge')
return 'return_p_hoge'
@property
def p_fuga(self):
print('in_p_fuga')
return 'return_p_fuga'
def f_hoge(self):
print('in_f_hoge')
return 'return_f_hoge'
def f_fuga(self):
print('in_f_fuga')
return 'return_f_fuga'
m = MyClass()
print('-' * 70)
print(m.p_hoge if m.always_true else m.p_fuga)
"""
in_always_true
in_p_hoge
return_p_hoge
"""
# p_fuga は評価されない。期待どおり。問題ない。
print('-' * 70)
print(m.f_hoge() if m.always_true else m.f_fuga())
"""
in_always_true
in_f_hoge
return_f_hoge
"""
# f_fuga は評価されない。期待どおり。問題ない。
print('-' * 70)
print(m.always_true or m.p_fuga)
"""
in_always_true
True
"""
# p_fuga は評価されない。期待どおり。問題ない。
print('-' * 70)
print(m.always_true or m.f_fuga())
"""
in_always_true
True
"""
# f_fuga は評価されない。期待どおり。問題ない。
print('-' * 70)
print(m.always_false and m.p_fuga)
"""
in_always_false
False
"""
# p_fuga は評価されない。期待どおり。問題ない。
print('-' * 70)
print(m.always_false and m.f_fuga())
"""
in_always_false
False
"""
# f_fuga は評価されない。期待どおり。問題ない。
print('-' * 70)
print(getattr(m, 'p_hoge', m.p_fuga))
"""
in_p_fuga
in_p_hoge
return_p_hoge
"""
# これは p_fuga は評価される
print('-' * 70)
print(getattr(m, 'p_hoge') or m.p_fuga)
"""
in_p_hoge
return_p_hoge
"""
# これは p_fuga は評価されない
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment