python - What does "self.default = default if default else type()" mean? -


i have been .net guy since started working (~6 years). recently, working on project using django, requires me learn python in parallel. lot of times bumped python code looked simple couldn't understand. , here 1 of them.

class typedproperty(object):     def __init__(self, name, type, default=none):         self.name = "_" + name         self.type = type         self.default = default if default else type() 

can kindly explain me last line of code doing?

i hope question not trivial, having trouble coming title it.

a if b else c conditional expression evaluates a if b evaluates true, , c otherwise. in given context, checks if default "trucy", in case stored in self.default. otherwise, new instance of type created , stroed in self.default.

note implementation has several issues:

  1. it uses type variable name, shadowing built-in type(). considered bad style.

  2. it uses condition if default if default none should used. check intended test if parameter default passed constructor. is, implementation won't accept default value evaluates false.


Comments