#๐ Typehint classes with TypedDict
25 messages ยท Page 1 of 1 (latest)
@halcyon mesa
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
what are you actually trying to accomplish by subclassing dict?
the usual way is to just use the TypedDict subclass
it's going to have few specific methods which dicts usually don't have
ok, why not just add those to your TypedDict subclass then?
it needs to inherit dict for using dict features
yup, I made a shorter example tho its not same
!e
from typing import TypedDict
class MyTypedDict(TypedDict):
key1: int
class MyDict(MyTypedDict):
def __init__(self, value):
super().__init__({
'key1': value - 10
})
foo = MyDict(20)
print(foo)
:x: Your 3.12 eval job has completed with return code 1.
001 | Traceback (most recent call last):
002 | File "/home/main.py", line 13, in <module>
003 | foo = MyDict(20)
004 | ^^^^^^^^^^
005 | TypeError: 'int' object is not iterable
this is where it goes wrong
that is an odd way to use dicts
why is your class a dict subclass at all?
if you insist on doing this, then you need to override __new__() instead of __init__()
let me try
like i said its for adding specific methods which perform operations on its items
are you otherwise using tMyDict as a dict? like you're using other dict methods too?
if not, then maybe implementing __getitem__() would be enough
yes i would be using most of dict methods
well i dont think i can pass arguments from new, like i can in init
WDYM?
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.