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:
it uses
typevariable name, shadowing built-intype(). considered bad style.it uses condition
if defaultif default noneshould used. check intended test if parameterdefaultpassed constructor. is, implementation won't accept default value evaluatesfalse.
Comments
Post a Comment