630 questions
0
votes
1
answer
79
views
Python tab completion triggers attribute access
I'm running into an issue with tab completion in Python. I have two classes, one serving as a backend that is responsible for managing i/o of a large data collection, and a second that serves as a UI ...
1
vote
1
answer
102
views
use __getattr__ within the same module or console
I have setup a custom __getattr__() method on a module which works perfectly when called from outside the module. However, it doesn't work from within the module itself (since that uses the globals() ...
1
vote
2
answers
123
views
Decorator with argument trying to access the self variable doesn't seem to extract self, but self.x instead
I have the following code involving a decorator decorating a property getter into a property-like object that reimplements mutation dunders to also invoke a specified function attribute fetched from ...
0
votes
0
answers
91
views
Why does VS Code sometimes colorise python print() as a magic method?
I didn't like VS Codes existing colour scheme for syntax highlighting in python, so I added rules using the textmate scopes. For functions, I used "entity.name.function.python". However, ...
0
votes
0
answers
145
views
Make a class currency convertor for 3 given currencies
I need to implement a currency converter for 3 given currencies + a couple of methods to manage them. Here are requirements:
Implement class Currency and inherited classes Euro, Dollar, and Pound. ...
1
vote
1
answer
101
views
Make a descriptor that will turn some class attributes to immutable after initialization
I need to make a descriptor that will turn some class attributes to immutable after initialization
I need to make 1 class and 2 descriptors that will manage its attributes. The task is:
You must ...
1
vote
1
answer
41
views
get true `__dict__` if `__dict__` was overridden
Is it possible to get object's true __dict__ if __dict__ was overridden? Are there simpler solutions than the ones below?
I came across this example where __dict__ was overridden and got curious. I ...
-1
votes
2
answers
114
views
How do I overwrite inherited method to raise a specific error?
Task:
Create a hierarchy out of birds. Implement four classes:
Class Bird with an attribute name and methods fly and walk.
Class FlyingBird with attributes name, ration, and with the same methods. ...
0
votes
1
answer
56
views
How to change connected class objects
My code:
class Potato:
def __init__(self, r):
self.size = r
def __itruediv__(self, other):
self.size /= other
return Potato(self.size)
def __truediv__(self, other)...
3
votes
1
answer
89
views
Weird Behavior of __new__
I have encountered a peculiar behavior with the __new__ method in Python and would like some clarification on its functionality in different scenarios. Let me illustrate with two unrelated classes, A ...
1
vote
0
answers
198
views
PhpStorm complains -- and doesn't -- about magic methods
The code IS working. The question is not about whether one should or should not use magic methods, its advantages vs disadvantages and what not. This question will probably be reviewed by a moderator, ...
0
votes
0
answers
23
views
Python: Can I get None's attribute from the descriptor's __get__?
I have this callable object bool_getter := vars(type(None))["__bool__"].__get__ (of type types.MethodWrapperType). Is there a way to make this bool_getter return None's method None.__bool__ ...
1
vote
2
answers
101
views
Can I get the name of the operator that triggered a special method?
I am working in customizing the behaviour of Python native functions and operators in my classes. I would like to avoid hard coding when printing error messages like "'<' not supported between ...
1
vote
1
answer
63
views
ImmutableDict implementation doesn't call __setitem__
In the code below I create a class ImmutableDict that appears to have a __setitem__ implementation that should throw an exception, but it doesn't. Why not?
from typing import Mapping, TypeVar
KT = ...
0
votes
2
answers
508
views
Why can I still do addition with a subtraction magic method?
This isn't as much of a problem as it is a genuine question coming from a place of ignorance of how Python works under the veil.
I saw a bit on magic methods and decided to try writing some code with ...