Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

learning python. learning defining functions . need help

44 views
Skip to first unread message

sigmaph...@gmail.com

unread,
Jul 21, 2016, 10:27:52 PM7/21/16
to
having a little trouble with defining functions. i have a doc called ch5.py and when i was trying the following i had issues

"
Try It Out: Defining a Function
Try saving the following in your file for Chapter 5, ch5.py.def in_fridge():


try:
count = fridge[wanted_food]
except KeyError:
count = 0
return count
How It Works

When you invoke ch5.py (press F5 while in Code Editor) with just the in_fridge function defined, you won't see any output. However, the function will be defined, and it can be invoked from the interactive Python session that you've created.

To take advantage of the in_fridge function, though, you have to ensure that there is a dictionary called fridge with food names in it. In addition, you have to have a string in the name wanted_food. This string is how you can ask, using in_fridge, whether that food is available. Therefore, from the interactive session, you can do this to use the function:


>>> fridge = {'apples':10, 'oranges':3, 'milk':2}
>>> wanted_food = 'apples'
>>> in_fridge()
10
>>> wanted_food = 'oranges'
>>> in_fridge()
3
>>> wanted_food = 'milk'
>>> in_fridge()
2"

---this is what I have and tried to run--
def in_fridge():

fridge = {'apples':10, 'oranges':3, 'milk':2}
wanted_food = 'apples'
in_fridge()


where am i going wrong

Chris Angelico

unread,
Jul 22, 2016, 9:24:43 AM7/22/16
to
On Fri, Jul 22, 2016 at 11:13 PM, Dennis Lee Bieber
<wlf...@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

justin walters

unread,
Jul 22, 2016, 11:21:37 AM7/22/16
to
> --
> 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:

Random832

unread,
Jul 22, 2016, 11:35:16 AM7/22/16
to
On Fri, Jul 22, 2016, at 11:21, justin walters wrote:
> Try opening the interactive terminal on your command line and type the
> following:
>
> type({}) == dict()
>
> That should illustrate why.

That doesn't illustrate anything relevant at all. The reason this is
false is because dict() is a dict instance, not the dict type.

type({}) == dict == type(dict()) will always* be true.

>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.

This is part of the language spec, it is not something that can be
chosen by each interpreter.

*well, assuming that "type" and "dict" have not been reassigned from
their built-in definitions.

Chris Angelico

unread,
Jul 22, 2016, 11:36:11 AM7/22/16
to
On Sat, Jul 23, 2016 at 1:21 AM, justin walters
<walters....@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

D'Arcy J.M. Cain

unread,
Jul 22, 2016, 2:07:02 PM7/22/16
to
On Fri, 22 Jul 2016 08:21:17 -0700
justin walters <walters....@gmail.com> wrote:
> You could also replace that line with:
>
> if stock is None or type(stock) != dict:

Use isinstance(). That handles classes that subclass dict as well.

--
D'Arcy J.M. Cain
System Administrator, Vex.Net
http://www.Vex.Net/ IM:da...@Vex.Net
VoIP: sip:da...@Vex.Net

MRAB

unread,
Jul 22, 2016, 2:37:14 PM7/22/16
to
On 2016-07-22 16:41, D'Arcy J.M. Cain wrote:
> On Fri, 22 Jul 2016 08:21:17 -0700
> justin walters <walters....@gmail.com> wrote:
>> You could also replace that line with:
>>
>> if stock is None or type(stock) != dict:
>
> Use isinstance(). That handles classes that subclass dict as well.
>
If you're checking that it's a dict, there's no need to check whether
it's None, because None isn't a dict either!

Steven D'Aprano

unread,
Jul 22, 2016, 9:45:10 PM7/22/16
to
On Sat, 23 Jul 2016 01:21 am, justin walters wrote:

> That should illustrate why. This is because simply typing '{}' could be
> interpreted as
> either a dict or a set.

No. {} is always an empty dict. That is a language guarantee. Any
programming language where {} is not an empty disk is not valid Python.

> My interpreter defaults 'type({})' to 'dict', but it's best to not
> take the risk.

Are you concerned that type([]) might not be list? Or type("") might not be
str? Or that type(0) might not be int?

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

Generally speaking, the right way to test whether something is an instance
of a type is to use the isinstance() function:

if stock is None or not isinstance(stock, dict): ...


That will work correctly even if stock belongs to a subclass of dict.



--
Steven
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.

0 new messages