user-defined-types

Python equivalent for typedef

Python equivalent for typedef Question: What is the python way to define a (non-class) type like: typedef Dict[Union[int, str], Set[str]] RecordType Asked By: OrenIshShalom || Source Answers: This would simply do it? from typing import Dict, Union, Set RecordType = Dict[Union[int, str], Set[str]] def my_func(rec: RecordType): pass my_func({1: {‘2’}}) my_func({1: {2}}) This code will generate …

Total answers: 2

Type hints with user defined classes

Type hints with user defined classes Question: Couldn’t seem to find a definitive answer. I want to do a type hint for a function and the type being some custom class that I have defined, called it CustomClass(). And then let’s say in some function, call it FuncA(arg), I have one argument named arg. Would …

Total answers: 2

What makes a user-defined class unhashable?

What makes a user-defined class unhashable? Question: The docs say that a class is hashable as long as it defines __hash__ method and __eq__ method. However: class X(list): # read-only interface of `tuple` and `list` should be the same, so reuse tuple.__hash__ __hash__ = tuple.__hash__ x1 = X() s = {x1} # TypeError: unhashable type: …

Total answers: 5