Skip to content
Advertisement

Tag: types

Type annotation for partial functions

we have multiple partial-like functions with same type annotation with args and kwargs like: can I somehow create a template for functions apple, orange, banana and assign it to them? I thought about Protocol with __call__ definition, but it is unclear how to assign it to functions Answer Rather than a function, you can use functools.partial: Another possibility would be

Python type hint for Iterable[str] that isn’t str

In Python, is there a way to distinguish between strings and other iterables of strings? A str is valid as an Iterable[str] type, but that may not be the correct input for a function. For example, in this trivial example that is intended to operate on sequences of filenames: Passing in a single filename would produce the wrong result but

Different types of inner classes in Python 2 and Python 3

I am upgrading my project from Python 2.7 to Python 3.7 and in my code I have something like this: now type(A.B()) returns different things in the two Python versions: In Python 2.7 I get: <class ‘__main__.B’> In Python 3.7 I get: <class ‘__main__.A.B’> Can anybody explain what happens under the hood that causes this […]

How to check if a python class or object is user defined (not a builtin)?

I need to examine a python module to find simple data as well as “user defined objects”. Here, I mean objects (or functions or classes) that are not builtin, but defined with a class/def statement or dynamically loaded/generated. Note there are similar questions, but they ask about “new style user defined classes”. I’ve seen the inspect module – I couldn’t

Advertisement