What's object name?

hi guys.
i’m wondering what’s included in python modules object name

from module_name import object_name

When done the way you have it, you’re just importing a function (or subpackage) of the module, rather than the entire module. Using import it searches for the included functions or subpackages.


from scipy import stats

Here, you’re just importing the stats subpackage of the SciPy module rather than the entire SciPy module.

And, when in doubt, check the docs:

1 Like

i know, it’s data imported from a full module. and we do it bcs we don’t want the whole module taking all over our space and process power. the question is, what can be there? functions ? namespaces? what else can be directly put in place of object name. what can the object be.

did you check out the documentation on __import__ & importlib ?

Regular packages and namespace packages.

1 Like

Take for example the math module. If we wish to see what modules it contains we can import it and then examine the directory:

>>> import math
>>> dir(math)
['__doc__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'comb', 'copysign', 'cos', 'cosh', 'degrees', 'dist', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'isqrt', 'lcm', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'nextafter', 'perm', 'pi', 'pow', 'prod', 'radians', 'remainder', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc', 'ulp']

Those methods with double underscore are called dunder methods which will have their own documentation to look up and read. Everything else is either a maths function, or in the case of pi and e, math constants.

With the module in memory, we can access all the attributes with dot syntax.

print (math.pi)    #  3.141592653589793
print (math.e)     #  2.718281828459045

When all we want is one method we can import it from the parent module:

from math import pi

print(pi)    #  3.141592653589793

When we import from, we can assign an alias:

from math import pi as PI

print (PI)    #  3.141592653589793

The same applies to all modules.

from random import randint as rand, random as rnd
>>> rand(1, 100)
>>> rand(1, 100)
>>> rnd()

Bear in mind that in Python, everything is a module. Note how it is described in the error message:

>>> for i in range(10):
...     print (n)
Traceback (most recent call last):
  File "<pyshell#28>", line 2, in <module>
    print (n)
NameError: name 'n' is not defined

The line in which the error occurs is a module, as we can see. So that means functions are modules, as is also the file we save that contains that function.

def square(x):
    return x ** 2
def cube(x):
    return x ** 3
def root(x, e=2):
    return x ** (1 / e)

Now we can save that to a file on our machine. I use the Scripts folder that comes with Python so I don’t have to tell Python where to look for it.

Save Scripts/maths.py

Now run the module so it is bound to the namespace. Then import it,

import maths

Any program can import that module and we have three built-in functions, maths.square(), maths.cube() and maths.root()

>>> maths.root(16)
>>> maths.root(81, 4)

We can also import from maths:

>>> from maths import root
>>> root(64, 6)

This is just an exploration in basic terms. Your reading should come up with a whole lot more information, demonstrations and usage examples.

One last thing to also note, if the module we import has any inline code not wrapped in functions or class objects, it will execute immediately. We’ve added a line to the file above,

print (dir())

Then we run it and import:

>>> import maths
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'cube', 'root', 'square']

We can go more into that once you are up to speed with the above and the other reading that @lisalisaj posted.


so basically anything can be in the object name’s place

1 Like

The module must exist, and the named object must exist within that module. Other than that, it can be whatever we wish to name it.