类接口作为模块接口

时间:2018-01-12 15:20:44

标签: python

我想要实现的是在模块级别拥有class Foo接口。

# foo.py
class Foo():
    def fun(self):
        pass

fun():
    return Foo().fun()  # this is what I want to avoid
                        # since module and class have
                        # the same interface

# module_scope_name = Foo()  # this is what I want to achieve

# SOLUTION:
fun = Foo().fun


# usage
import foo
foo.fun()

1 个答案:

答案 0 :(得分:2)

Stop writing classes

foo.py:

def fun():
    pass

用法:

import foo
foo.fun()