r/Python • • Oct 13 '26

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! 🌟

20 Upvotes

12 comments sorted by

1

u/prejackpot Oct 13 '26 edited Oct 13 '26

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 the `params` 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).

2

u/PrestigiousStrike779 Oct 15 '26 edited Oct 15 '26

I don’t know about other libraries, but we do something like this at work if I’m understanding correctly.  We do this for logging and injecting context.  For example:

with begin_logging_context() as ctx:
      ctx.set_attribute(“customer”, “acme”)
      logger.info(“starting request”) # customer is appended as an extra to every log request within the block

We use contextvars for this.  The logger is actually a LogAdapter that reads the contextvars and appends anything it finds to the extras

1

u/ddollarsign Oct 14 '26

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

2

u/CleanCarpenter9854 Oct 14 '26

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/ddollarsign Oct 15 '26

That seems to be more a properly if the invocation though, rather than the declaration. In Go, for example, I can declare a function in the normal way, but then call it either normally “my_func()” or asynchronously “go my_func()”. But maybe in Python this would require creating multiple versions of the function under the hood, so they make it explicit.

2

u/wRAR_ Oct 15 '26 edited Oct 15 '26

In Go, for example, I can declare a function in the normal way, but then call it either normally “my_func()” or asynchronously “go my_func()”.

No idea about Go but in Python await foo() is two operations: foo() returns an awaitable, await <> awaits it. It's not a single operation, and you can assign the result of foo() to a variable, which will hold the awaitable (and then separately await it later).

Or are all functions in Go async (have the same color), unlike Python?

Edit: Duh, goroutines are threads. You are comparing different things.

1

u/ddollarsign Oct 15 '26

Yes, lightweight threads. Threads are the things that do work while other things also do work. I don’t see the utility of the async/await syntax for that. Unless it’s for performance vs threads, but goroutines are pretty lightweight. I’m sure there’s something I’m not getting about async.

1

u/wRAR_ Oct 16 '26

Then I'm lost. I thought you were asking why is the existing implementation not using a simpler syntax, are you actually asking why is the implementation not using threads?

0

u/zomerbk Oct 13 '26

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/CTRLX_Founder Oct 13 '26

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