Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #111762

Re: learning python. learning defining functions . need help

From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Subject Re: learning python. learning defining functions . need help
Date 2016-07-23 01:35 +1000
Message-ID <mailman.57.1469201759.22221.python-list@python.org> (permalink)
References <432aeb03-ea14-4ece-8e5b-a7521cf12b84@googlegroups.com> <ji44pbpbaj6olkqcq832ivm7q7gl5d1aca@4ax.com> <CAPTjJmq6nnqp3434HNkxZLPm-igt-vk76u1GZVsr7euhWd4f8Q@mail.gmail.com> <CAO1D73GxUMSwRZvggewY8o-3GLpSMZHAyGD18by1dy-CqyGhpw@mail.gmail.com> <CAPTjJmrsCAccd5y9d4kJwU0peSgvCWEgKc_pN4kswhOkvQpG5Q@mail.gmail.com>

Show all headers | View raw


On Sat, Jul 23, 2016 at 1:21 AM, justin walters
<walters.justin01@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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

learning python. learning defining functions . need help sigmaphine1914@gmail.com - 2016-07-21 19:27 -0700
  Re: learning python. learning defining functions . need help Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2016-07-22 09:13 -0400
  Re: learning python. learning defining functions . need help Chris Angelico <rosuav@gmail.com> - 2016-07-22 23:24 +1000
  Re: learning python. learning defining functions . need help justin walters <walters.justin01@gmail.com> - 2016-07-22 08:21 -0700
    Re: learning python. learning defining functions . need help Steven D'Aprano <steve@pearwood.info> - 2016-07-23 11:44 +1000
  Re: learning python. learning defining functions . need help Random832 <random832@fastmail.com> - 2016-07-22 11:35 -0400
  Re: learning python. learning defining functions . need help Chris Angelico <rosuav@gmail.com> - 2016-07-23 01:35 +1000
  Re: learning python. learning defining functions . need help "D'Arcy J.M. Cain" <darcy@Vex.Net> - 2016-07-22 11:41 -0400
  Re: learning python. learning defining functions . need help MRAB <python@mrabarnett.plus.com> - 2016-07-22 19:36 +0100
  Re: learning python. learning defining functions . need help Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2016-07-22 20:04 -0400
  Re: learning python. learning defining functions . need help Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2016-07-23 11:39 -0400

csiph-web