Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python import as tuple

Tags:

python

django

What's the difference between

from package import x, y 

and

from package import (x, y)

I've seen this usage in Django source code but couldn't find the documentation for the second method (using round brackets around import)

  • https://github.com/django/django/blob/master/django/contrib/auth/views.py#L5
  • https://github.com/django/django/blob/master/django/contrib/auth/models.py#L13
like image 284
user Avatar asked Apr 03 '14 05:04

user


People also ask

How do you make a tuple in Python?

Creating a Tuple A tuple is created by placing all the items (elements) inside parentheses () , separated by commas. The parentheses are optional, however, it is a good practice to use them. A tuple can have any number of items and they may be of different types (integer, float, list, string, etc.).

Why would you use a tuple in Python?

Tuples are used to store multiple items in a single variable. Tuple is one of 4 built-in data types in Python used to store collections of data, the other 3 are List, Set, and Dictionary, all with different qualities and usage. A tuple is a collection which is ordered and unchangeable.

How do you turn a list into a tuple?

Using the tuple() built-in function An iterable can be passed as an input to the tuple () function, which will convert it to a tuple object. If you want to convert a Python list to a tuple, you can use the tuple() function to pass the full list as an argument, and it will return the tuple data type as an output.


1 Answers

The statements are functionally equivalent.

From http://legacy.python.org/dev/peps/pep-0328/, the use of parentheses was approved for enclosing long lists of imports in a pythonic way:

from Tkinter import (Tk, Frame, Button, Entry, Canvas, Text,
    LEFT, DISABLED, NORMAL, RIDGE, END)

It seems that parentheses were added for the second statement because the import list was too long.

like image 188
Blue Ice Avatar answered Sep 30 '22 13:09

Blue Ice