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


Groups > comp.lang.python > #111759

Re: learning python. learning defining functions . need help

From justin walters <walters.justin01@gmail.com>
Newsgroups comp.lang.python
Subject Re: learning python. learning defining functions . need help
Date 2016-07-22 08:21 -0700
Message-ID <mailman.55.1469200885.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>

Show all headers | View raw


On Fri, Jul 22, 2016 at 6:24 AM, Chris Angelico <rosuav@gmail.com> wrote:

> On Fri, Jul 22, 2016 at 11:13 PM, Dennis Lee Bieber
> <wlfraed@ix.netcom.com> wrote:
> >         Now... Going much beyond the assignment (if you were having
> trouble
> > with the assignment, this will seem like magic) [Python 2.7]:
>
> I'm not sure, but I think your code would become Py3 compatible if you
> just change your prints. Which I'd recommend - it's not difficult to
> just always print a single string, and most example code is ASCII-only
> and has no difficulty with the bytes/unicode distinction. But, point
> of curiosity...
>
> > class Refrigerator(object):
> >     def __init__(self, stock=None):
> >         if stock is None or type(stock) != type(dict()):
> >             self._stock = dict()
> >         else:
> >             self._stock = stock
>
> ... why do you call up "type(dict())"? Why not either just "dict" or
> "type({})"?
>
> ChrisA
> --
> https://mail.python.org/mailman/listinfo/python-list
>

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.

You could also replace that line with:

    if stock is None or type(stock) != dict:

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