Python default equivalence in C #

Is there a way in python to get the default value for types?

//C# default(typeof(int)) 

Am I looking for a more pythonic way to get the default settings?

 #python if(isinstance(myObj, int): return 0 elif(isinstance(myObj, dict): return {} else: return None 

Obviously, I lowered it. I am dealing with some abstract things, and when someone requests an attribute that I don’t have, I basically check for a key-> type mapping and return a default instance with a classic switch.

+4
source share
2 answers

Just create an instance:

 int() # 0 dict() # {} list() # [] 

More: in Python there is no explicit notion of "default value". Just an instance of the class created with default parameters. Some classes may expect arguments when they are created, in which case the default value does not matter.

+11
source

What about:

 type(myObj)() 

It works for int and dict.

+3
source

All Articles