I am learning python and I came across with package in python. I made a directory containing __init__.py and shipping.py. In shipping.py the code is
def calc_shipping():
print("calculating shipping cost")
I made another python file called app.py outside this directory to import this package
from package import shipping
#package.shipping.calc_shipping()
shipping.calc_shipping()
I want to know that when we called shipping.calc_shipping() it prints the message.The shipping in this is an object ? if it is then we should have class, we didn't declare any classes in our package. Somebody please clarify me with this concept
CodePudding user response:
In Python everything is an object. classes, functions, methods, even modules, operators and etc. (except for language tokens and keywords which make statements like if-statement , try-except... )
Every class in Python inherits from a base object class.
Functions are not exceptions, They are from type function:
def fn():
pass
print(type(fn))
output:
<class 'function'>
You don't need to define it in your module. def keyword will create an instance of this class for you.
It is available through types module:
from types import FunctionType
def fn():
pass
t = type(fn)
print(t is FunctionType) # True
