Python Practice Creating and Using Functions in Python Practice Functions Use an External Function

Luke Tate
seal-mask
.a{fill-rule:evenodd;}techdegree
Luke Tate
Python Development Techdegree Student 2,243 Points

What does the from mean for functions?

using_a_function.py
"""
This is importing a function named `tweet` from a file
    that we unfortunately don't have access to change.

You use it like so:
>>> tweet("Hello this is my tweet")

If the function cannot connect to Twitter,
    the function will raise a `CommunicationError`
If the message is too long,
    the function will raise a `MessageTooLongError`
"""
from twitter import (
    tweet,
    MessageTooLongError,
    CommunicationError,
)


message = input("What would you like to tweet?  ")
# Your code here

4 Answers

Rick Gleitz
Rick Gleitz
41,835 Points

You are importing the tweet, MessageTooLongError, and CommunicationError functions from the twitter library. There are LOTS of libraries in python from which you can import either the whole library, or just specific functions like these. I heard in an older podcast that there are over 90,000 libraries available! The purpose is that these libraries let you access code that someone else has written so you don't have to recreate the wheel. Hope this helps!

Rick Gleitz
Rick Gleitz
41,835 Points

Typing in the "from twitter import" statement, "from ... import" allows you to import specific elements such as functions or attributes of a library, module, or API (like twitter). The word from is a keyword in python used for this purpose, as is import. I hope this makes it more clear. There are a lot of jargony terms, some of which aren't obvious. They have specific meanings, but they often tend to be muddled together (at least in my mind). I hope I've done them justice.

Check these links for further info (they helped me clarify my thoughts here): https://www.programiz.com/python-programming/modules https://www.programiz.com/python-programming/keyword-list#from_import