What does it mean to put the 'as' command after the 'import' command in the code below?

Asked

Viewed 646 times

8

'as' is in the send_to_twitter function (msg)

import urllib.request
import time

def send_to_twitter(msg):
    import twitter as t:
    CONSUMER_KEY = '1wrnsF5lB8fEWVzRdvlIqdTle'
    CONSUMER_SECRET = 'eTiylDUHLJgGnTCcxzzCtzHXG4OlHrbY6wLvuZUnAEhrokyNAF'
    ACCESS_KEY = '2325915097-q2JYaZ3UGeL9Pr95BJC7643NMyETY6x7Bb8T1q1'
    ACCESS_SECRET = '8GRq4e9ukVKcC8XjroM3iLKuZYOM2QtFEdCHXG3TXx0zo'
    auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
    auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
    api = tweepy.API(auth)
    api.update_status(msg)
    print(msg)

def get_price():
    page = urllib.request.urlopen("http://beans-r-us.appspot.com/prices-loyalty.html")
    text = page.read().decode("utf8")
    where = text.find('>$')
    start_of_price = where + 2
    end_of_price = start_of_price + 4
    return(text[start_of_price : end_of_price])

escolha = input("Precisa do valor imediatamente? ")

if escolha == 'y':
    send_to_twitter(get_price())
else:
    if escolha == 'n':
        price = 99.99
        while price > 4.74:
            price = float(get_price())
            time.sleep(3)
        send_to_twitter('BUY!')
    else:
        print("Voce nao escolheu nada")

3 answers

11


According to in the documentation the as in import twitter as t: in Python means that you are creating an alias for the imported module.

import without the practice of the:

>>>import math
>>>math.pi
>>>3.141592653589793
etc..

import with the practice of the:

>>>import math as m
>>>m.pi
>>>3.141592653589793
etc..

You can read more on Importing Modules.

Observation by Matheus Saraiva:

The most practical use of this is that you can give names more significant to an imported class or function and use the new name in its code instead of the original name. I also use in cases where the name of an imported function is a bit large so I create the alias for not have to type that function with huge name every time need to use it.

  • 3

    Add-on: The most practical utility of this is that you can give more meaningful names to an imported class or function and use the new name in your code instead of the original name. I also use in cases where the name of an imported function is a bit large so I create the alias so I don’t have to type that function with a huge name every time I need to use it.

6

As @Acklay explained, you can use the word as as a nickname for the imported module in Python.

This syntax was proposed by PEP 221 and as explained in this document, it was not intended to be a Python keyword, but is included in the list of keywords in that language.

Print the keyword list in Python 2.7.*:

>>> import keyword
>>> print(keyword.kwlist)

as is used to create an alias while importing a module. It Means Giving a Different name (user-defined) to a module while importing it.

Source: Programiz - List Of Keywords In Python Programming

0

It means "how"

import os as so

in this case you are saying "Import the module os and change your name to that code as so"

so you can call his methods by the name you gave the module:

os.chdir()

it is useful to give the names you want and also to decrease the name of some modules that can be too big to be writing all the time, for example we can have a module with the name meuModuloPython, that name too big, so we can call it mp (python module) used the as.

import meuModuloPython as mp
mp.fazalgo()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.