PyQuant News

PQN #009: How to ask questions that get Great Answers

PQN #009: How to ask questions that get Great Answers

How to ask questions that get Great Answers

In today’s issue, I’m going to take a break from technical content. I want to introduce you to a framework I developed to help you avoid burning out and quitting.

The secret of the best Python developers is they are good at asking questions. Asking questions the right way – in the right place – will help you get Great Answers. Great Answers help you maintain momentum, stay positive, and avoid quitting.

Unfortunately for most people getting started with Python, it goes something like this:

  • Get stuck on a problem (or error)
  • Lose momentum & motivation
  • Search aimlessly on Google
  • Get unhelpful answers
  • Spin their wheels more
  • Feel terrible
  • Quit

I’ve been there. And it sucks.

I’ve been asking questions – some good, some bad – for 10 years. I’ve distilled my experience into a simple framework to help you get Great Answers.

I want to share it with you.

Step 1: Search and research

If you get an error, start by pasting the error message into Google.

Here’s an example of a common Python error called a “KeyError” exception. It happens when you ask for a key that doesn’t exist in a dictionary.

>>> my_dict['my_key']
Traceback (most recent call last):
  File "", line 1, in 
KeyError: 'my_key'

I start typing the error message into Google and sure enough, I’m not alone:

PQN #009: How to ask questions that get Great Answers

I pick the first option and there are 3,400,000 results.

PQN #009: How to ask questions that get Great Answers

Spend a few minutes looking for answers on Google. There’s a good chance someone else had the same problem as you. You can use Google Advanced search to get even more specific.

Pro tip: Keep track of what you find if you decide to post a question.

Step 2: Sign up for a free account at Stack Overflow

Stack Overflow is a community of 18.6 million developers. They are asking and answering questions every second of every day. There is no better place on the internet than Stack Overflow to find answers to your questions fast.

(Most of the time when searching google, you’ll be linked to Stack Overflow anyway.)

So head over to https://stackoverflow.com/ and register an account.

Pro tip: One way to make a good impression is to give yourself a meaningful username. Use your real name if you’re comfortable with it. If not, something humble. Calling yourself “epic_developer” or “god-of-code” is a good way to get ignored.

Step 3: Post your question

Get Great Answers

A well-written question on Stack Overflow will get you a Great Answer in seconds. A poorly written question on Stack Overflow will get ignored.

This is how to write a question to get Great Answers.

Write a meaningful title

Your title should summarize the problem you’re trying to solve. If it’s not clear, or interesting, people won’t read it.

Do your best to form the title as a question. Make it something someone can answer. Imagine you’re talking to a busy colleague and have to sum up the entire question in one sentence. Include as much detail as you can in the most compact way you can.

Spelling, grammar, and punctuation are important. Make a good impression. Use clear language. Your audience is technical so technical language and jargon are OK. Make sure you use the common conventions of Python.

If you’re struggling, write the title last. Sometimes you see the entire question laid out the title comes naturally.

Here are some examples:

Bad: Why doesn’t this work?

Good: Why do I get a Python ValueError exception when calling int()?

Bad: Python dictionary question

Good: How do I return the first N key:value pairs from a Python dict?

Bad: Python and APIs

Good: Why do I get a socket exception when calling this API from Python?

The good questions are very specific and describe the problem.

If in doubt, try these templates:

  • How do I with/from a?
  • Why do I get when calling

Whether your title is a question, follows a template, or uses correct grammar (all important) THE MOST important part is that it is CLEAR.

Introduce the problem and provide context

You need to set the stage for people reading your question. Dumping a bunch of code into the question doesn’t help anyone. The best questions provide context into the background of the problem and the outcome you’re expecting.

For example:

I’m following along with the PyMongo tutorial and am getting an error when calling the insert_one method on a collection. I’m using the exact code in the tutorial so I do not expect to get an error. I’m trying to insert a record into my database.

Can you tell what the person is trying to do? Yes! They’re trying to insert a record based on a tutorial.

Finally, make sure you make it obvious what you’re trying to get out of the question. A lot of questions are actually statements: when I do X, this happens. It’s not clear what the question actually is.

To improve on the context from above, add a specific request for help:

Why am I getting this error?

This is an open-ended question that makes clear what I’m asking for. No ambiguity.

Help others reproduce the problem with sample code

Not all questions benefit from including code, but if your problem is with code you’ve written, you should include it. But don’t just copy in your entire program:

  • There’s a risk you’re making private IP public
  • It may not run on someone else’s computer (not reproducible)
  • Irrelevant details add noise and make it hard to understand the question

Here are some guidelines:

  • Include just enough code to allow others to reproduce the problem
  • Use the <code> tags in the editor and follow Python’s formatting conventions
  • Do not paste images of code – make it easy for people to copy and paste your code
  • Simplify the code to strip out anything unnecessary keeping only the part you have a question about

In short, make it as easy as possible for someone else to run your code.

Extending the above, here’s an example:

In [1]: import pymongo

In [2]: from pymongo import MongoClient

In [3]: client = MongoClient()

In [4]: db = client.new_db

In [5]: db
Out[5]: Database(MongoClient('localhost', 27017), u'new_db')

In [6]: posts = db.posts

In [7]: posts.insert_one({'a':1})
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
 in ()
----> 1 posts.insert_one({'a':1})

C:\Anaconda\lib\site-packages\pymongo-2.8-py2.7-win32.egg\pymongo\collection.py in __call__(self, *a
rgs, **kwargs)
   1771                         "call the '%s' method on a 'Collection' object it is "
   1772                         "failing because no such method exists." %
-> 1773                         self.__name.split(".")[-1])

TypeError: 'Collection' object is not callable. If you meant to call the 'insert_one' method on a 'Collection' object it is failing because no such method exists.

This question is directly related to the pymongo library. Assuming they have it installed, replicating this problem is simple.

Include what you’ve tried

Since everyone is learning, it’s important to show that you’ve tried to solve your own problem. If there’s a perception that you dumped your problem on others without trying to solve it yourself, you won’t get a Great Answer.

Include relevant tags

Include a tag for the language, library, or specific API your question relates to. If you start typing in the tags field, the system will suggest tags that match what you’ve typed. Read the descriptions to make sure they’re relevant to the question you’re asking.

Proofread

Now that you’re ready to post your question, take a break then read through it from start to finish. Pretend you’re seeing it for the first time: does it make sense? Can you reproduce the problem yourself in a fresh environment? Add any details you missed and read through it again. Now is a good time to make sure that your title still describes the problem.

You might find the act of writing out the problem helps you solve it yourself. This happens about 25% of the time for me.

Take a deep breath. Hit post!

Make a good impression

Congratulations! You now belong to the community. Make a good impression to increase your chances of further success.

Here’s how:

  • Watch your question. There might be comments asking for clarification, edits, or answers. If you receive an answer that doesn’t actually answer, help the person with (very) kind comments thanking them and telling them why it doesn’t hit the mark.
  • Skip the greetings. Don’t include greetings and sign-offs like “Hi everyone” and “hope to get an answer soon”. People are on Stack Overflow for one reason.
  • Be polite. No one is getting paid. Be appreciate that people are giving up their time to help you. Don’t take comments personally. A lot of people use clipped tones in writing – it’s not about you.
  • Upvote an answer if you use it. Stack Overflow has a reputation system. If someone answers your question and you use the answer, upvote their question to help build their reputation.

I’ll get back to the technical stuff next week 🙂

Well, that’s it for today. I hope you enjoyed it.

See you again next week.

Join your Fellow Pythonistas on PyQuant News

Follow Us

Recent Posts

Related Posts