在python中的函数内定义本地类是一个坏主意吗? [英] Is it a bad idea to define a local class inside a function in python?

查看:39
本文介绍了在python中的函数内定义本地类是一个坏主意吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不久前我正在研究的一个函数的结构如下:

One function I was working on not too long ago had a structure like so:

def function():
    class Inner:
        #class stuff
    #function stuff

Inner 只在 function 内部使用和需要,它也不会在函数结束时返回.定义一个本地类是一个坏主意吗?我听说过很多关于它对性能有什么不利的事情,因为每次运行该函数时 python 都必须重新编译该类,而且由于性能是我的目标是使用这个函数实现的,所以我有点担心这样做.

Inner is only ever used and needed inside of function, and it isn't returned at the end of the function either. Is it a bad idea to define a local class? I've heard many things about how it's bad for performance since python has to recompile the class every single time the function is run, and since performance is what I'm aiming to achieve with this function I'm a little worried about doing this.

推荐答案

在您的案例中定义本地类似乎没有用.我会这样做如果确实想要退货.定义本地类时有一些缺点:

Defining a local class in your case seems useless. I'd do that if I did want to return it. There some disadvantages when defining local classes:

  • 可读性:要么类真的很琐碎,要么函数不可避免地变得很长,其逻辑在类声明中丢失.此外,如果您在某处有一些嵌套循环,您还有一个额外的缩进级别,这可能会损害可读性
  • 性能:类将在每次函数调用时重新构建.这通常不会花费大量时间,但会花费一些.如果您运行的函数速度很快,则此成本可能会很高.

定义本地类还有一些优点:

There are also some advantages of defining a local class:

  • 局部性:您通常非常确定该类不会以您意想不到的方式在函数外使用
  • 性能:查找局部变量比查找全局变量要快得多.如果您创建大量实例,这可能会提高使用全局类的性能.然而,通过默认参数/局部变量来抵消这种优势真的很容易.

我的建议是简单地全局定义类,如果它应该是私有的,则使用以下划线开头的名称,例如 _MyClass,因为这是用于表示私有项的约定.

My suggestion would be to simply define the class globally and, if it should be private, use a name that starts with an underscore, like _MyClass, since this is the convention used to denote private items.

给出性能变化的一些时间:

Some timings to give an idea of the changes in performance:

In [1]: class _Out(object):
   ...:     def test(self):
   ...:         for _ in range(10):
   ...:             pass
   ...:         

In [2]: def function_out(n):
   ...:     for _ in range(n):
   ...:         _Out().test()
   ...:         

In [3]: def function_in(n):
   ...:     class Inner(object):
   ...:         def test(self):
   ...:             for _ in range(10):
   ...:                 pass
   ...:     for _ in range(n):
   ...:         Inner().test()
   ...:         

In [4]: def function_mixed(n, cls=_Out):
   ...:     # use of default to access the global class via local variable
   ...:     for _ in range(n):
   ...:         cls().test()
   ...:         

In [5]: %timeit function_out(1000)
1000 loops, best of 3: 602 us per loop

In [6]: %timeit function_in(1000)
1000 loops, best of 3: 621 us per loop

In [7]: %timeit function_mixed(1000)
1000 loops, best of 3: 590 us per loop

In [8]: %timeit function_out(100000)
10 loops, best of 3: 59.9 ms per loop

In [9]: %timeit function_in(100000)
10 loops, best of 3: 60.2 ms per loop

In [10]: %timeit function_mixed(100000)
10 loops, best of 3: 58.4 ms per loop

In [11]: %timeit function_out(10)
100000 loops, best of 3: 6.52 us per loop

In [12]: %timeit function_in(10)
10000 loops, best of 3: 57.8 us per loop

In [13]: %timeit function_mixed(10)
100000 loops, best of 3: 6.33 us per loop

注意如何在迭代次数较多时 function_infunction_out 大约同时运行,而在迭代次数较少时 function_in 是大约慢 10 倍.

Note how with a big number of iterations function_in and function_out run in about the same time, while with small number of iterations function_in is about 10 times slower.

这篇关于在python中的函数内定义本地类是一个坏主意吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆