Working through the single responsibility principle (SRP) in Python when calls are expensive

Some base points:

  • Python method calls are “expensive” due to its interpreted nature. In theory, if your code is simple enough, breaking down Python code has negative impact besides readability and reuse (which is a big gain for developers, not so much for users).
  • The single responsibility principle (SRP) keeps code readable, is easier to test and maintain.
  • The project has a special kind of background where we want readable code, tests, and time performance.

For instance, code like this which invokes several methods (x4) is slower than the following one which is just one.

from operator import add  class Vector:     def __init__(self,list_of_3):         self.coordinates = list_of_3      def move(self,movement):         self.coordinates = list( map(add, self.coordinates, movement))         return self.coordinates      def revert(self):         self.coordinates = self.coordinates[::-1]         return self.coordinates      def get_coordinates(self):         return self.coordinates  ## Operation with one vector vec3 = Vector([1,2,3]) vec3.move([1,1,1]) vec3.revert() vec3.get_coordinates() 

In comparison to this:

from operator import add  def move_and_revert_and_return(vector,movement):     return list( map(add, vector, movement) )[::-1]  move_and_revert_and_return([1,2,3],[1,1,1]) 

If I am to parallelize something such as that, it is pretty objective I lose performance. Mind that is just an example; my project has several mini routines with math such as that – While it is much easier to work with, our profilers are disliking it.


How and where do we embrace the SRP without compromising performance in Python, as its inherent implementation directly impacts it?

Are there workarounds, like some sort of pre-processor that puts things in-line for release?

Or is Python simply poor at handling code breakdown altogether?

Will renting a car ad hoc in NYC be much more expensive than booking it beforehand?

I will be in NYC and upstate from late June to early July. I’m planning to rent a car for the second half of the stay, but it could turn out I need one earlier.

Should I absolutely book a car now to make sure I will get one at a reasonable price, or can I wait until there and rent one on the same day without a huge change in price?

In smaller places in Europe I would be nervous about short-term availability. Is this an issue in NYC?

Why are so many kit-lenses parfocal if it’s an expensive feature?

According to this answer, parfocal lenses (lenses that can zoom in/out without losing focus) are relatively rare and more expensive.

However, the original poster’s kit-lens is parfocal. Additionally, the kit-lens that came with my Sony a390 and the kit lens with my brother’s Nikon D3200 (both entry-level DSLR’s, with presumably cheapo kit-lenses) are parfocal as well.

Does anyone know why, if this is a feature primarily of expensive lenses, so many cheapo kit-lenses seem to support it? What features of the lens cause it to be parfocal or not?

Expensive combinatorial optimization of choice of subset from a large finite space

I have a fairly general question — what’s a good (gradient-free) approach to optimizing an expensive function whose input is a choice of subset from a large finite population?

That is, I have a set $ X$ , an integer $ n$ , and a function $ F: Y \to\mathbb{R}$ , where $ Y$ is the set of all $ n$ -element subsets of $ X$ (and $ |X| >> n$ ). Knowing nothing about F (in fact, assume it’s expensive and free of any helpful structure. In the actual use case it’s noisy as well, though I’m interested in a non-noisy answer too), what are good options to maximize $ F$ on $ Y$ ?

I’m in the process of doing this using a random search-style approach, where the choice of next subset is made by fixing some $ m<n$ and redrawing $ m$ elements of the subset at each step; I think that “number of elements not in common” constitutes a metric on $ Y$ , so it seems like this is sound, but it also looks naive to me. This problem seems pretty general and useful, so I’d love to be pointed to some other ideas.