#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@author: XiangguoSun
@contact: sunxiangguodut@qq.com
@file: suggest2.py
@time: 2017/5/2 10:46
@software: PyCharm
"""
# ex1: 三元操作符 condition? value1:value2
x = 0
y = -2
z = x if x < y else y
# ex2: switch...case
# method1:
n = 2
if n == 0:
print('Your typed 0.\n')
elif n == 1:
print('Your typed 0.\n')
elif n == 2:
print('Your typed 2.\n')
else:
print('Only single-digit accept.\n')
# method2:
def switch(case):
"""
switch-case code block
:param case: your case
:return: specific function in your case
"""
return {
0: 'Your typed 0.\n',
1: 'Your typed 1.\n',
2: 'Your typed 2.\n'
}.get(case, 'Only single-digit accept.\n') # get your input case and decide which function to do.
print(switch(1))
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@author: XiangguoSun
@contact: sunxiangguodut@qq.com
@file: const.py
@time: 2017/5/2 11:43
@software: PyCharm
"""
class _const:
class ConstError(TypeError):
pass
class ConstCaseError(ConstError):
pass
def __setattr__(self, name, value):
if name in self.__dict__:
raise(self.ConstError, "Can't change const.{name}".format(name=name))
if not name.isupper():
raise(self.ConstCaseError, "const name {name} is not all uppercase".format(name=name))
self.__dict__[name] = value
import sys
sys.modules[__name__] = _const()
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@author: XiangguoSun
@contact: sunxiangguodut@qq.com
@file: constant.py
@time: 2017/5/2 11:30
@software: PyCharm
"""
import const
const.MY_CONSTANT = 1
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@author: XiangguoSun
@contact: sunxiangguodut@qq.com
@file: TEST.py
@time: 2017/5/2 11:42
@software: PyCharm
"""
from constant import const
print(const.MY_CONSTANT)