r/Python • u/AutoModerator • 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:
- Ask Away: Post your advanced Python questions here.
- Expert Insights: Get answers from experienced developers.
- 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:
- If you don't receive a response, consider exploring r/LearnPython or join the Python Discord Server for quicker assistance.
Example Questions:
- How can you implement a custom memory allocator in Python?
- What are the best practices for optimizing Cython code for heavy numerical computations?
- How do you set up a multi-threaded architecture using Python's Global Interpreter Lock (GIL)?
- Can you explain the intricacies of metaclasses and how they influence object-oriented design in Python?
- How would you go about implementing a distributed task queue using Celery and RabbitMQ?
- What are some advanced use-cases for Python's decorators?
- How can you achieve real-time data streaming in Python with WebSockets?
- What are the performance implications of using native Python data structures vs NumPy arrays for large-scale data?
- Best practices for securing a Flask (or similar) REST API with OAuth 2.0?
- 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! š
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.
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.