Python fayl import | files and OOP Class import in Python | from import alias #kody_az

Dərs haqqında

Python-un .py (Python) faylları ilə işləyərkən və onları layihənizə necə “daxil” (import) edərkən bilməli olduğunuz bir neçə məqam var.

Python faylları: Skriptlər və Modul(lar)
1. Skript
Bir .py faylı birbaşa işlədildikdə (məsələn, python myscript.py əmrini icra etdikdə) o, skript kimi fəaliyyət göstərir. Fayldakı bütün kodlar (global scope-da yazılmış hissələr) dərhal icra olunur.
2. Modul
Başqa bir .py faylından “import” edildikdə, bu .py faylı modul sayılır. Bu modul funksiyalar, dəyişənlər, siniflər və s. saxlayır və onların hamısı digər Python fayllarında istifadə oluna bilər.

Qeyd: Bir .py faylı həm skript, həm də modul kimi davrana bilər. Birbaşa işlətsəniz – skript kimi, “import” edilsə – modul kimi fəaliyyət göstərir.

Müəyyən Adları (Funksiyaları, Sinifləri) İdxal Etmək
Moduldan birbaşa müəyyən funksiyaları və ya sinifləri çağırmaq üçün:
from ... import ...
misal: from mymodule import greet

Ləqəbdən İstifadə (Aliasing)
Bəzən modul adını qısaltmaq və ya başqa adla istifadə etmək üçün ləqəb verilir:
import mymodule as mm
və ya
from mymodule import greet as g

Wildcard Import (Tövsiyə Olunmur)
from mymodule import *
Bu üsul mymodule içərisindəki bütün funksiyaları, dəyişənləri, sinifləri cari “namespace”-ə gətirir. Ad toqquşmaları və kodun aydınlığının azalması ehtimalı yüksək olduğu üçün bu metod ümumiyyətlə məsləhət görülmür.

Tövsiyələr (Best Practices)
1. Import-ları faylın yuxarısında saxlayın (PEP 8 standartlarına uyğundur).
2. .py fayllarınıza məna dolu adlar verin ki, nə etdikləri aydın olsun.
3. Yalnız ehtiyac duyduğunuz funksiyaları və sinifləri “from module import MyClass” üsulu ilə idxal edin.
4. import * (from module import *) istifadəsini minimuma endirin.

=====

There are a few important points to know when working with Python .py (Python) files and including (importing) them into your project.

Python Files: Scripts and Modules

1. Script

When a .py file is run directly (for example, by executing python myscript.py), it acts as a script. All the code in the file (the parts written in the global scope) is executed immediately.

2. Module

When a .py file is imported from another .py file, that .py file is considered a module. This module contains functions, variables, classes, etc., all of which can be used in other Python files.

Note: A single .py file can act as both a script and a module. If you run it directly, it behaves like a script; if it is imported, it behaves like a module.

Importing Specific Names (Functions, Classes)
To directly call certain functions or classes from a module, use:
from ... import ...
from mymodule import greet

Using an Alias (Aliasing)
Sometimes, you might shorten the module name or use a different name when importing:
import mymodule as mm
or
from mymodule import greet as g

Wildcard Import (Not Recommended)
from mymodule import *
This method imports all functions, variables, and classes from mymodule into the current namespace. Because it can lead to name collisions and reduced code clarity, it is generally not advised.

Recommendations (Best Practices)
1. Keep your imports at the top of the file (following PEP 8 standards).
2. Give meaningful names to your .py files so it’s clear what they do.
3. Only import the functions and classes you need using:
from module import MyClass
4. Minimize the usage of import * (i.e., from module import *).

#python #kody_az #kodyaz #viral #fleet #jetbrains #pycharm #viralcode #viralvideo #random #code #coder #azerbaycan #azerbaijan #azərbaycan #baku #baki #programming #programmer #codes #hazırlıq #hazirliq #kurs #kurslar #course #courses #online #onlinecourses #test #quiz

YouTube-da bax · KODY.AZ