modules and imports
This commit is contained in:
4
.gitignore
vendored
4
.gitignore
vendored
@@ -1 +1,3 @@
|
|||||||
.venv
|
.venv
|
||||||
|
__pycache__/
|
||||||
|
*.pyc
|
||||||
28
src/basic/file1.py
Normal file
28
src/basic/file1.py
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
|
||||||
|
# All functions, variables, class are available from export
|
||||||
|
# Python exports everything(functions, variables, classes) by default
|
||||||
|
# See file3 for custome Exporting
|
||||||
|
from file3 import *
|
||||||
|
|
||||||
|
|
||||||
|
def print_my_name():
|
||||||
|
print("Anikuttan")
|
||||||
|
|
||||||
|
|
||||||
|
file1_variable = 30
|
||||||
|
|
||||||
|
|
||||||
|
class Ani:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
# ==================================================================
|
||||||
|
|
||||||
|
|
||||||
|
print_my_full_name()
|
||||||
|
|
||||||
|
hidden_function() # hidden_function" is not defined error
|
||||||
|
|
||||||
|
print(my_name)
|
||||||
|
|
||||||
|
nai = AniKuttan()
|
||||||
35
src/basic/file2.py
Normal file
35
src/basic/file2.py
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
|
||||||
|
|
||||||
|
# Import the whole module
|
||||||
|
# We must use the module name as a prefix
|
||||||
|
import file1
|
||||||
|
|
||||||
|
file1.print_my_name()
|
||||||
|
print(file1.file1_variable)
|
||||||
|
|
||||||
|
# ==================================================================
|
||||||
|
|
||||||
|
# Import specific functions/variables
|
||||||
|
# No prefix needed
|
||||||
|
|
||||||
|
from file1 import print_my_name
|
||||||
|
|
||||||
|
print_my_name()
|
||||||
|
|
||||||
|
# ==================================================================
|
||||||
|
|
||||||
|
# Import with alias
|
||||||
|
|
||||||
|
import file1 as my_file
|
||||||
|
|
||||||
|
my_file.print_my_name()
|
||||||
|
|
||||||
|
#==================================================================
|
||||||
|
|
||||||
|
# Import all
|
||||||
|
from file1 import *
|
||||||
|
|
||||||
|
print_my_name()
|
||||||
|
file1_variable
|
||||||
|
|
||||||
|
my = Ani()
|
||||||
16
src/basic/file3.py
Normal file
16
src/basic/file3.py
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
__all__ = ["print_my_full_name", "my_name", "AniKuttan"]
|
||||||
|
|
||||||
|
|
||||||
|
def print_my_full_name():
|
||||||
|
print("Anikuttan Vijayakumar from file3.py")
|
||||||
|
|
||||||
|
|
||||||
|
my_name = "ANIKUTTAN from file3.py"
|
||||||
|
|
||||||
|
|
||||||
|
def hidden_function():
|
||||||
|
print("You shouldn't see me")
|
||||||
|
|
||||||
|
|
||||||
|
class AniKuttan:
|
||||||
|
pass
|
||||||
12
src/basic/file4.py
Normal file
12
src/basic/file4.py
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
# Here we are importing the function from the service module
|
||||||
|
# services is the module (package) name order_service is the file name
|
||||||
|
# package is a folder with multiple .py files and an __init__.py file
|
||||||
|
from services.order_service import my_order_one
|
||||||
|
from services.product_service import helper_product_from_product_file
|
||||||
|
from services import my_product_one
|
||||||
|
|
||||||
|
# from services import helper_product_from_product_file
|
||||||
|
|
||||||
|
# my_order_one()
|
||||||
|
my_product_one()
|
||||||
|
helper_product_from_product_file()
|
||||||
@@ -115,3 +115,15 @@ def add(a, b) -> int:
|
|||||||
|
|
||||||
number = add(a=10, b=20)
|
number = add(a=10, b=20)
|
||||||
print(number)
|
print(number)
|
||||||
|
|
||||||
|
|
||||||
|
def say_name():
|
||||||
|
...
|
||||||
|
|
||||||
|
|
||||||
|
def say_name():
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
v = 10
|
||||||
|
dn = 40
|
||||||
@@ -64,15 +64,15 @@
|
|||||||
# print(x, y)
|
# print(x, y)
|
||||||
|
|
||||||
|
|
||||||
# students = [{
|
students = [{
|
||||||
# "name": "Ani",
|
"name": "Ani",
|
||||||
# "subject": "Tamil"
|
"subject": "Tamil"
|
||||||
|
|
||||||
# }, {
|
}, {
|
||||||
# "name": "Kuttan",
|
"name": "Kuttan",
|
||||||
# "subject": "English"
|
"subject": "English"
|
||||||
|
|
||||||
# }]
|
}]
|
||||||
|
|
||||||
# for value in students:
|
# for value in students:
|
||||||
# print(value['name'])
|
# print(value['name'])
|
||||||
|
|||||||
23
src/basic/my_script/script_1.py
Normal file
23
src/basic/my_script/script_1.py
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
# Example 1
|
||||||
|
|
||||||
|
# if i run this file directly the name will call once becaus python will run the file top to bottom
|
||||||
|
# so the function call will trigger and it prints my name
|
||||||
|
def say_my_name_once():
|
||||||
|
print("Anikuttan example 1")
|
||||||
|
print(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
say_my_name_once()
|
||||||
|
|
||||||
|
# -----------------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
# Example 2
|
||||||
|
|
||||||
|
|
||||||
|
def say_my_name_once_example2():
|
||||||
|
print("Anikuttan example 2")
|
||||||
|
|
||||||
|
|
||||||
|
# see Script_2.py
|
||||||
|
if __name__ == "__main__":
|
||||||
|
say_my_name_once()
|
||||||
18
src/basic/my_script/script_2.py
Normal file
18
src/basic/my_script/script_2.py
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
# Example 1
|
||||||
|
|
||||||
|
# in here if i run this file script_2.py file it will print my name 2 times
|
||||||
|
# because the say_my_name_once triggers first time in this file and 2 time will call from the script_1.py file Example 1
|
||||||
|
from script_1 import say_my_name_once_example2
|
||||||
|
from script_1 import say_my_name_once
|
||||||
|
|
||||||
|
say_my_name_once()
|
||||||
|
|
||||||
|
|
||||||
|
# -----------------------------------------------------------
|
||||||
|
|
||||||
|
# Example 2
|
||||||
|
|
||||||
|
|
||||||
|
say_my_name_once_example2() # this will call one because the script_1.py file Example 2 there is __name__ condition
|
||||||
|
# __name__ will be "__main__" if the file runs directly
|
||||||
|
# if module runs by other file the __name__ will be "own modules name" like script1
|
||||||
16
src/basic/services/__init__.py
Normal file
16
src/basic/services/__init__.py
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
# It’s a special file that makes Python treat a directory as a package.
|
||||||
|
# it can be empty or we can exports with __all__
|
||||||
|
|
||||||
|
from .product_service import my_product_one
|
||||||
|
|
||||||
|
__all__ = ["my_product_one"] # see below
|
||||||
|
|
||||||
|
|
||||||
|
# in file4.py the imports look like this
|
||||||
|
# 1. from services import my_product_one
|
||||||
|
# 2. from services import helper_product_from_product_file # this will give error [cannot import name 'helper_product_from_product_file' from 'services']
|
||||||
|
|
||||||
|
# to use helper_product_from_product_file function or class we can able to access by package name (module name) like
|
||||||
|
# ==> from services.product_service import helper_product_from_product_file
|
||||||
|
# But we can able to access the my_product_one function like this
|
||||||
|
# ==> from services import my_product_one
|
||||||
6
src/basic/services/order_service.py
Normal file
6
src/basic/services/order_service.py
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
def my_order_one():
|
||||||
|
print("This i from order_service.py")
|
||||||
|
|
||||||
|
|
||||||
|
def helper_service_from_order_file():
|
||||||
|
print("My service from order service")
|
||||||
8
src/basic/services/product_service.py
Normal file
8
src/basic/services/product_service.py
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
# see services/__init__.py file and file4.py
|
||||||
|
|
||||||
|
def my_product_one():
|
||||||
|
print("This i from order_service.py --> my_product_one")
|
||||||
|
|
||||||
|
|
||||||
|
def helper_product_from_product_file():
|
||||||
|
print("My service from order service --> helper_product_from_product_file")
|
||||||
Reference in New Issue
Block a user