Object is not callable python что это
Перейти к содержимому

Object is not callable python что это

TypeError ‘module’ object is not callable in Python

There are many in-built functions in Python, offering a variety of operations. All these functions are within modules or libraries. So, if you want to use a function that is within a module, you need to specify the module within the program where the function resides. Modules are included in Python programs using the import statement. For example,

This statement will include the math module within your program. You can use the functions such as factorial(), floor() and fabs() within this module. But if you try using a function with name math(), the compiler will be confused. It will throw an error called TypeError ‘module’ object is not callable in Python.
Here, we will focus on a solution to this problem.

In Python, all inbuilt function is provided by modules, therefore to use any function within the module, we need to include that module in our code file.

Note: Module is the collection of code library in a categorical manner.

What is TypeError ‘module’ object is not callable in Python

This error statement TypeError: ‘module’ object is not callable occurs when the user gets confused between Class name and Module name. The issue occurs in the import line while importing a module as module name and class name have the same name.

Cause of this Error

The error “TypeError: ‘module’ object is not callable” occurs when the python compiler gets confused between function name and module name and try to run a module name as a function.

Example:

Output:

In the above example, we have imported the module “os” and then try to run the same “os” module name as a function.

And as we can see In the module “os” there is no function with the name “os” therefore TypeError: ‘module’ object is not callable” is thrown.

Example with Custom module and function

To explain this error, we will create a module and function with the same name.

File name: mymodule.py

Code:

In the above code we have created a file name “mymodule.py” and in that file created a function with the name “mymodule”

We can see that the module name and the function name in the above code is the same.

File name: mycode.py

Code:

Output:

In the above code, we are trying to call a function named “mymodule” which is in module “mymodule”, due to the similar name of module and function and due to this our python compiler gets confused and throws the following error

TypeError: ‘module’ object is not callable

How to fix typeerror: ‘module’ object is not callable?

To fix this error, we need to change the import statement in “mycode.py” file and specify a specific function in our import statement.

Example:

Output:

In the above code, we have imported “mymodule” function from “mymodule” module, so our compiler won’t get confused and can execute the function mymodule().

[Solved] TypeError: ‘Module’ Object Is Not Callable in Python?

TypeError: ‘Module’ Object Is Not Callable in Python

In this article we will discuss: TypeError: ‘Module’ Object Is Not Callable in Python and the methods to overcome such errors in your program.

Example:

Consider that you have created the following module with the name Palindrome.py :

Now, you want to use this module in another program and check if an entered value is Palindrome or not :

Let’s execute this code and check the output:

Wait. �� Why did we get this error? It brings us to a number of questions:

  • What is a TypeError ?
  • What does TypeError: module object is not callable mean?
  • Why did we encounter this error in our program?
  • How to fix TypeError: module object is not callable ?

Let us dive into each question in details and find out the correct way of calling a certain module and its attributes.

◈ What is TypeError in Python?

A TypeError is raised when a certain operation is applied to an object of an incorrect type. For example, if you try to add a string object and an integer object using the + operator, then you will encounter a TypeError because the + operation is not allowed between the two objects that are f different types.

Example:

Output:

  • Trying to perform an unsupported operation between two types of objects.
  • Trying to call a non-callable caller.
  • Trying to iterate over a non-iterative identifier.

Now that we have a clear idea about TypeErrors , let us understand what does TypeError: module object is not callable mean?

What Does TypeError: module object is not callable Mean?

TypeError: ‘module’ object is not callable is generally raised when you are trying to access a module as a function within your program. This happens when you are confused between a module name and a class name/function name within that module. For example:

Output:

In the above example, we have imported the module socket; however, within the program, we are trying to use it as a function that ultimately leads to TypeError: ‘module’ object is not callable . The user got confused because the module name and the class name are the same.

◈ Therefore we now have an idea about the reason behind the occurrence of TypeError: ‘module’ object is not callable. So, why did we encounter this error in our example?

The reason is already known to us from the above explanation. If you have a look at the example, you will notice that the name of the module is Palindrome while it has a function with the name Palindrome as well. So, that’s what created the confusion!

That brings us to the all important question. How do we fix the error?

◈ Fix: TypeError: ‘module’ object is not callable

There are couple of ways you can use to fix this error.

Method 1: Change The “ import ” Statement

Instead of importing the module you can import the function or attribute within the module to avoid the TypeError . Let’s have a look at the solution to our example scenario in the program given below.

Метод callable() в Python

Метод callable() возвращает True, если переданный объект кажется вызываемым. Если нет, возвращается False.

Параметры

Метод callable() принимает объект с одним аргументом.

Возвращаемое значение

callable() в Python возвращает:

  • Истина — если объект кажется вызываемым
  • Ложь — если объект не вызывается.

Важно помнить, что даже если callable() имеет значение True, вызов объекта все равно может завершиться ошибкой.

Однако, если callable() возвращает False, вызов объекта обязательно завершится ошибкой.

Пример 1: Как работает callable()?

Здесь объект x не вызывается. И объект y кажется вызываемым (но не может быть вызван).

Пример 2: вызываемый объект

Экземпляр класса Foo кажется вызываемым (и в этом случае вызывается).

Пример 3: объект выглядит вызываемым, но не вызывается

Экземпляр класса Foo кажется вызываемым, но не вызываемым. Следующий код вызовет ошибку.

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *