learning python. learning defining functions . need help

justin walters walters.justin01 at gmail.com
Fri Jul 22 11:21:17 EDT 2016


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

> On Fri, Jul 22, 2016 at 11:13 PM, Dennis Lee Bieber
> <wlfraed at 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:



More information about the Python-list mailing list