How can I cast a var
into a CustomClass
?
In Python, I can use float(var)
, int(var)
and str(var)
to cast a variable into primitive data types but I can't use CustomClass(var)
to cast a variable into a CustomClass
unless I have a constructor for that variable type.
Example with inheritance.
class CustomBase:
pass
class CustomClass(CustomBase):
def foo():
pass
def bar(var: CustomBase):
if isinstance(var, CustomClass):
# customClass = CustomClass(var) <-- Would like to cast here...
# customClass.foo() <-- to make it clear that I can call foo here.