Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing with dot notation

Can someone explain this to me? When you import Tkinter.Messagebox what actually does this mean (Dot Notation)? I know that you can import Tkinter but when you import Tkinter.Messagebox what actually is this? Is it a class inside a class?

I am new to Python and dot notation confuses me sometimes.

like image 451
Kwnstantinos Nikoloutsos Avatar asked Aug 09 '17 14:08

Kwnstantinos Nikoloutsos


People also ask

What does dot mean in from import?

Relative imports make use of dot notation to specify location: A single dot means that the module or package referenced is in the same directory as the current location. Two dots mean that it is in the parent directory of the current location, in other words the directory above.

What is from DOT import in Python?

The dot (.) symbol after in an import statement of the form from . import your_module is a Python syntactical element for relative imports. It means “look for the module in your current folder”.

What is a Python file with DOT?

They contain user preferences and application configurations. Your editor, terminal, Python environment, Git, and other tools you use daily most probably create dotfiles. These files could be the most important files on your computer. Since they have a dot in the file name, they are called dotfiles.

How do you do dot notation in Python?

When you use dot notation, you indicate to Python that you want to either run a particular operation on, or to access a particular property of, an object type. Python knows how to infer the object type on which this operation is being run because you use dot notation on an object.


2 Answers

When you're putting that dot in your imports, you're referring to something inside the package/file you're importing from. what you import can be a class, package or a file, each time you put a dot you ask something that is inside the instance before it.

parent/
    __init__.py
    file.py
    one/
        __init__.py
        anotherfile.py
    two/
        __init__.py
    three/
        __init__.py

for example you have this, when you pass import parent.file you're actually importing another python module that may contain classes and variables, so to refer to a specific variable or class inside that file you do from parent.file import class for example.

this may go further, import a packaging inside another package or a class inside a file inside a package etc (like import parent.one.anotherfile) For more info read Python documentation about this.

like image 103
WiGeeky Avatar answered Oct 03 '22 22:10

WiGeeky


import a.b imports b into the namespace a, you can access it by a.b . Be aware that this only works if b is a module. (e.g. import urllib.request in Python 3)

from a import b however imports b into the current namespace, accessible by b. This works for classes, functions etc.

Be careful when using from - import:

from math import sqrt
from cmath import sqrt

Both statements import the function sqrt into the current namespace, however, the second import statement overrides the first one.

like image 43
Fulgen Avatar answered Oct 04 '22 00:10

Fulgen