dynamic-typing

Python. Use object attributes to annotate methods; Dynamically-defined type annotations

Python. Use object attributes to annotate methods; Dynamically-defined type annotations Question: Python pseudocode I want to dynamically annotate returning types for my methods. I want something that looks like this: class Foo: def __init__(model: BaseModel): self.model = model def find_model(query) -> self.model: return db.get(query) And then use it like this: bar = Foo(model=MyModel) # pass …

Total answers: 1

How do I define a `typing.Union` dynamically?

How do I define a `typing.Union` dynamically? Question: I am using Typeguard in a couple if projects for type checking at run time in Python. It works pretty well. I have encountered a situation where the type of a function parameter is a typing.Union made up of a few dynamically collected data types. E.g. def …

Total answers: 2

Is Python type safe?

Is Python type safe? Question: According to Wikipedia Computer scientists consider a language “type-safe” if it does not allow operations or conversions that violate the rules of the type system. Since Python runtime checks ensure that type system rules are satisfied, we should consider Python a type safe language. The same point is made by …

Total answers: 8

Do union types actually exist in python?

Do union types actually exist in python? Question: Since python is dynamically typed, of course we can do something like this: def f(x): return 2 if x else “s” But is this the way python was actually intended to be used? Or in other words, do union types exist in the sense they do in …

Total answers: 6

How does Python interpreter work in dynamic typing?

How does Python interpreter work in dynamic typing? Question: I read this question, but it didn’t give me a clear answer: How does Python interpreter look for types? How does python interpreter know the type of a variable? I’m not looking how do get the type. I’m here looking at what happens behind the scene. …

Total answers: 3

How to identify numpy types in python?

How to identify numpy types in python? Question: How can one reliably determine if an object has a numpy type? I realize that this question goes against the philosophy of duck typing, but idea is to make sure a function (which uses scipy and numpy) never returns a numpy type unless it is called with …

Total answers: 6

How can I type-check variables in Python?

How can I type-check variables in Python? Question: I have a Python function that takes a numeric argument that must be an integer in order for it behave correctly. What is the preferred way of verifying this in Python? My first reaction is to do something like this: def isInteger(n): return int(n) == n But …

Total answers: 9

Using Variables for Class Names in Python?

Using Variables for Class Names in Python? Question: I want to know how to use variables for objects and function names in Python. In PHP, you can do this: $className = “MyClass”; $newObject = new $className(); How do you do this sort of thing in Python? Or, am I totally not appreciating some fundamental difference …

Total answers: 6