Thanks to visit codestin.com
Credit goes to www.tutorialsteacher.com

Tutorialsteacher

Follow Us

Articles
  • C#
  • C# OOP
  • ASP.NET Core
  • ASP.NET MVC
  • LINQ
  • Inversion of Control (IoC)
  • Web API
  • JavaScript
  • TypeScript
  • jQuery
  • Angular 11
  • Node.js
  • D3.js
  • Sass
  • Python
  • Go lang
  • HTTPS (SSL)
  • Regex
  • SQL
  • SQL Server
  • PostgreSQL
  • MongoDB
  • Python - Get Started
  • What is Python?
  • Where to use Python?
  • Python Version History
  • Install Python
  • Python - Shell/REPL
  • Python IDLE
  • Python Editors
  • Python Syntax
  • Python Keywords
  • Python Variables
  • Python Data Types
  • Number
  • String
  • List
  • Tuple
  • Set
  • Dictionary
  • Python Operators
  • Python Conditions - if, elif
  • Python While Loop
  • Python For Loop
  • User Defined Functions
  • Lambda Functions
  • Variable Scope
  • Python Modules
  • Module Attributes
  • Python Packages
  • Python PIP
  • __main__, __name__
  • Python Built-in Modules
  • OS Module
  • Sys Module
  • Math Module
  • Statistics Module
  • Collections Module
  • Random Module
  • Python Generator Function
  • Python List Comprehension
  • Python Recursion
  • Python Built-in Error Types
  • Python Exception Handling
  • Python Assert Statement
  • Define Class in Python
  • Inheritance in Python
  • Python Access Modifiers
  • Python Decorators
  • @property Decorator
  • @classmethod Decorator
  • @staticmethod Decorator
  • Python Dunder Methods
  • CRUD Operations in Python
  • Python Read, Write Files
  • Regex in Python
  • Create GUI using Tkinter
Entity Framework Extensions - Boost EF Core 9
  Bulk Insert
  Bulk Delete
  Bulk Update
  Bulk Merge

Python Module Attributes: name, doc, file, dict

Python module has its attributes that describes it. Attributes perform some tasks or contain some information about the module. Some of the important attributes are explained below:

__name__ Attribute

The __name__ attribute returns the name of the module. By default, the name of the file (excluding the extension .py) is the value of __name__attribute.

Example: __name__ Attribute
import math
print(math.__name__)  #'math'
Try it

In the same way, it gives the name of your custom module e.g. calc module will return 'calc'.

Example: __name__ Attribute
import calc 
print(calc.__name__)  #'calc'

However, this can be modified by assigning different strings to this attribute. Change hello.py as shown below.

Example: Set __name__
def SayHello(name):
    print ("Hi {}! How are you?".format(name))
__name__="SayHello"

And check the __name__ attribute now.

Example: Set __name__
import hello
print(hello.__name__)  #'SayHello'
Try it

The value of the __name__ attribute is __main__ on the Python interactive shell and in the main.py module.

Example: __main__
print(__name__)
Try it

When we run any Python script (i.e. a module), its __name__ attribute is also set to __main__. For example, create the following welcome.py in IDLE.

Example: welcome.py
print("__name__ = ", __name__)

Run the above welcome.py in IDLE by pressing F5. You will see the following result.

Output in IDLE:
__name__ =  __main__

However, when this module is imported, its __name__ is set to its filename. Now, import the welcome module in the new file test.py with the following content.

Example: test.py
import welcome
print("__name__ = ", __name__)

Now run the test.py in IDLE by pressing F5. The __name__ attribute is now "welcome".

Example: test.py
__name__ =  welcome

This attribute allows a Python script to be used as an executable or as a module.

Visit __main__ in Python for more information.

__doc__ Attribute

The __doc__ attribute denotes the documentation string (docstring) line written in a module code.

Example:
import math  

print(math.__doc__)
Try it

Consider the the following script is saved as greet.py module.

greet.py
"""This is docstring of test module"""
def SayHello(name):
    print ("Hi {}! How are you?".format(name))
            return

The __doc__ attribute will return a string defined at the beginning of the module code.

Example: __doc__
import greet

print(greet.__doc__)
Try it

__file__ Attribute

__file__ is an optional attribute which holds the name and path of the module file from which it is loaded.

Example: __file__ Attribute
import io

print(io.__file__) #output: 'C:\python37\lib\io.py'
Try it

__dict__ Attribute

The __dict__ attribute will return a dictionary object of module attributes, functions and other definitions and their respective values.

Example: __dict__ Attribute
import math

print(math.__dict__)
Try it

The dir() is a built-in function that also returns the list of all attributes and functions in a module.

Example: dir()
import math

print(dir("math"))
Try it

You can use the dir() function in IDLE too, as shown below.

Know Module Attributes and Methods

Learn more about module attributes in Python Docs.

TUTORIALSTEACHER.COM

TutorialsTeacher.com is your authoritative source for comprehensive technologies tutorials, tailored to guide you through mastering various web and other technologies through a step-by-step approach.

Our content helps you to learn technologies easily and quickly for learners of all levels. By accessing this platform, you acknowledge that you have reviewed and consented to abide by our Terms of Use and Privacy Policy, designed to safeguard your experience and privacy rights.

[email protected]

ABOUT USTERMS OF USEPRIVACY POLICY
copywrite-symbol

2024 TutorialsTeacher.com. (v 1.2) All Rights Reserved.