Descriptors vs Property

Ian Kelly ian.g.kelly at gmail.com
Sat Mar 12 01:37:11 EST 2016


On Fri, Mar 11, 2016 at 11:24 PM, Veek. M <vek.m1234 at gmail.com> wrote:
> Ian Kelly wrote:
>
>> On Fri, Mar 11, 2016 at 10:59 PM, Veek. M <vek.m1234 at gmail.com> wrote:
>>> Also, what's this bit:
>>> self.default = default if default else type()
>>
>> If the default parameter has a truthy value, it gets set to
>> self.default. Otherwise, the type parameter is called with no
>> arguments, and the resulting instance is used as self.default instead.
>
> But type() just gives me:
> TypeError: type() takes 1 or 3 arguments
> on py2,3

You're using the built-in type. In the example, type is the name of
one of the arguments, so its value is whatever was passed in.
Presumably it's meant to be the type that is being checked, and the
result of calling it with no arguments is meant to be a default
instance of that type.

This is not a particularly great example, to be honest. It shadows a
builtin, it makes unwarranted assumptions about the passed-in type,
and the "default if default else type()" expression does not account
for the possibility that the user might actually want to pass in a
default that evaluates as false.



More information about the Python-list mailing list