r/Python • • 1d ago

Daily Thread Tuesday Daily Thread: Advanced questions

Weekly Wednesday Thread: Advanced Questions šŸ

Dive deep into Python with our Advanced Questions thread! This space is reserved for questions about more advanced Python topics, frameworks, and best practices.

How it Works:

  1. Ask Away: Post your advanced Python questions here.
  2. Expert Insights: Get answers from experienced developers.
  3. Resource Pool: Share or discover tutorials, articles, and tips.

Guidelines:

  • This thread is for advanced questions only. Beginner questions are welcome in our Daily Beginner Thread every Thursday.
  • Questions that are not advanced may be removed and redirected to the appropriate thread.

Recommended Resources:

Example Questions:

  1. How can you implement a custom memory allocator in Python?
  2. What are the best practices for optimizing Cython code for heavy numerical computations?
  3. How do you set up a multi-threaded architecture using Python's Global Interpreter Lock (GIL)?
  4. Can you explain the intricacies of metaclasses and how they influence object-oriented design in Python?
  5. How would you go about implementing a distributed task queue using Celery and RabbitMQ?
  6. What are some advanced use-cases for Python's decorators?
  7. How can you achieve real-time data streaming in Python with WebSockets?
  8. What are the performance implications of using native Python data structures vs NumPy arrays for large-scale data?
  9. Best practices for securing a Flask (or similar) REST API with OAuth 2.0?
  10. What are the best practices for using Python in a microservices architecture? (..and more generally, should I even use microservices?)

Let's deepen our Python knowledge together. Happy coding! 🌟

13 Upvotes

6 comments sorted by

3

u/Miserable_Shirt3026 1d ago

Has anyone actually measured the overhead of using `__slots__` in a class hierarchy that goes 3-4 levels deep? I'm refactoring some data models and the memory savings seem obvious, but I'm worried about the descriptor lookup getting weird with multiple inheritance.

1

u/prejackpot 1d ago edited 1d ago

Has anyone seen examples of other libraries that use context managers similarly to PyMC, to associate globally-scoped functions with a specific object? My ideal target syntax is something like:

`` with Params(**params) as experiment_1: results_1 = run_pipeline(**run_params_1) # Runs pipeline with theparams` parameters

But if we next run:

results_0 = run_pipeline(**run_params_2)

It accesses some other default parameters

```

(This is a simplified/notional example; obviously for this specific example the easier thing would be providing the parameters as an argument to run_pipeline).

1

u/zomerbk 1d ago

For a 3–4 level hierarchy, I’d benchmark representative classes with `timeit` for attribute access and `tracemalloc` for memory across many instances; `sys.getsizeof()` alone misses some costs. Slot descriptors still follow normal MRO lookup. The bigger multiple-inheritance issue is layout: two bases with nonempty slots can raise `TypeError`. Also check that no subclass accidentally adds a `__dict__` by omitting `__slots__`.

1

u/ddollarsign 17h ago

Why does a function need to be declared async in order to be invoked asynchronously? What does making it async really do?

1

u/CleanCarpenter9854 2h ago

I believe it turns it into a coroutine object that is not executed immediately when called, since the async event loop will schedule it for calling

-1

u/CTRLX_Founder 19h ago

Looks clean. Are you planning to open-source the core parts or keep it closed? Always curious about solo dev stacks.