learning python. learning defining functions . need help

Chris Angelico rosuav at gmail.com
Fri Jul 22 11:35:56 EDT 2016


On Sat, Jul 23, 2016 at 1:21 AM, justin walters
<walters.justin01 at gmail.com> wrote:
> Hi Chris,
>
> Try opening the interactive terminal on your command line and type the
> following:
>
>     type({}) == dict()
>
> That should illustrate why. This is because simply typing '{}' could be
> interpreted as
> either a dict or a set. My interpreter defaults 'type({})' to 'dict', but
> it's best to not
> take the risk.

Of course the type of {} is not equal to an empty dict, but it *will*
be equal to dict itself:

>>> type({}) == dict
True

And it's not ambiguous; it's always going to mean a dictionary.
There's no empty set syntax in Python (well, not as of 3.6, anyway).

> You could also replace that line with:
>
>     if stock is None or type(stock) != dict:

That's exactly what I was saying. Barring shenanigans (like shadowing
'dict' with a function or something), type(dict()) will always be
dict.

ChrisA



More information about the Python-list mailing list