Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

python_repos.py #34

Closed
tianlpark opened this issue May 7, 2017 · 1 comment
Closed

python_repos.py #34

tianlpark opened this issue May 7, 2017 · 1 comment

Comments

@tianlpark
Copy link

Hi Eric,
I am reading your Python Crash Course and am currently at Chapter 17, Working with APIs.

For the python_repos.py example, I wanted to use the following code:
description = repo_dict.get('description', 'No description provided')
Instead of:
description = repo_dict['description']
if not description:
description = 'No description provided.'

But this gives me an error code:
"NoneType" object has not attribute 'decode'.

Why is this?

Thank you!

Best,
Tian

@ehmatthes
Copy link
Owner

Hi @tianlpark,

If you print the dictionary, you'll find this entry as part of the dictionary:

'description': None,

So the key 'description' is in the dictionary. When you run

description = repo_dict.get('description', 'No description provided')

Python grabs the value associated with the key 'description', which is None. This is different than the key not being there. It's a subtle issue, and I'm happy to explain that more carefully if it's not clear.

By the way, here's the code snippet I used to figure out why you were getting that error:

names, plot_dicts = [], []
for repo_dict in repo_dicts:
    names.append(repo_dict['name'])

    # Get the project description, if one is available.
    # description = repo_dict['description']
    # if not description:
    #     description = "No description provided."
    description = repo_dict.get('description', 'blah')
    if not description:
    	print('dict:', repo_dict)
    	print('description:', description)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants