improve your python code(2)

来源:互联网 发布:飞利浦e5音响 知乎 编辑:程序博客网 时间:2024/05/16 10:47
#!/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:value2x = 0y = -2z = x if x < y else y# ex2: switch...case# method1:n = 2if 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] = valueimport syssys.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 constconst.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 constprint(const.MY_CONSTANT)
0 0
原创粉丝点击