Pylint false positives

Marko Rauhamaa marko at pacujo.net
Mon Aug 20 08:58:57 EDT 2018


Gregory Ewing <greg.ewing at canterbury.ac.nz>:
> Marko Rauhamaa wrote:
>> Some of these chores are heavier, some of them are lighter. But where
>> I have used Python, performance hasn't been a bottleneck. It it were,
>> I'd choose different approaches of implementation.
>
> The point is that creating a class object every time you want a
> closure is pointlessly wasteful. There is *no benefit whatsoever* in
> doing that. If you think there is, then it's probably because you're
> trying to write Java programs in Python.

The benefit, as in using closures in general, is in the idiom.

>> But now I'm thinking the original Java approach (anonymous inner
>> classes) is probably the most versatile of them all. A single
>> function rarely captures behavior. That's the job of an object with
>> its multiple methods. In in order to create an ad-hoc object in
>> Python, you will need an ad-hoc class.
>
> An important difference between Python and Java here is that in Python
> the class statement is an *executable* statement, whereas in Java it's
> just a declaration. So putting a class statement inside a Python
> function incurs a large runtime overhead that you don't get with a
> Java inner class.

The same is true for inner def statements.

I don't see how creating a class would be fundamentally slower to
execute than, say, adding two integers. It may be the CPython has not
been optimized with the inner class use case. And it may be that
Python's data model has painted CPython into a corner, which then would
call the data model into question.

Anyway, in practice on my laptop it takes 7 µs to execute a class
statement, which is clearly worse than executing a def statement (0.1
µs) or integer addition (0.05 µs). However, 7 microseconds is the least
of my programming concerns.


Marko



More information about the Python-list mailing list