-->
With Examples fix: AttributeError: 'function' object has no attribute 'glob and AttributeError: 'function' object has no attribute 'get_extra_actions Python

With Examples fix: AttributeError: 'function' object has no attribute 'glob and AttributeError: 'function' object has no attribute 'get_extra_actions Python

Back to top

Updated by Ashirafu Kibalama on September 17, 2024

Why do you see "AttributeError: 'function' object has no attribute 'glob and AttributeError: 'function' object has no attribute 'get_extra_actions Python."




Fixing the AttributeError: 'function' object has no attribute 'glob' and AttributeError: 'function' object has no attribute 'get_extra_actions' in Python can be straightforward. These errors normally occur when functions or methods are mistakenly assigned to variables with conflicting names. Learn how to resolve or fix these issues with real world examples below.


Fix or Resolve AttributeError: 'function' object has no attribute 'glob Python With Examples:




The AttributeError: 'function' object has no attribute 'glob' in Python, which typically occurs when you mistakenly override a module or a class with a function with the same name. This is common when dealing with the glob module in Python.


For example:

Codes Before fixing the AttributeError: 'function' object has no attribute 'glob Python


import glob


# Some function named `glob` that you defined
def glob():
pass


# Trying to use `glob.glob` will now cause an error because `glob` is redefined as a function
files = glob.glob("*.txt")


Output:





In this case, Python tries to access the glob function you defined rather than the glob module, which causes the AttributeError because a function object does not have a glob attribute.


2 Ways To Fix or Resolve the AttributeError: 'function' object has no attribute 'glob Python with Examples:


1) Rename your function:

Ensure that any function or variable you define doesn't have the same name as an imported module.



import glob

# Rename your function
def my_glob_function():
pass


files = glob.glob("*.txt")


In this corrected version, the glob module is correctly imported and used, and the custom function is defined under a different name to avoid conflicts.


2) Import with alias:

If you need a function named glob, you can import the glob module under a different alias.



# Import with alias
import glob as glob_module


def glob():
pass


files = glob_module.glob("*.txt")


This will prevent the conflict and resolve the error.


Fix or Resolve AttributeError: 'function' object has no attribute 'get_extra_actions Python With Examples:




The AttributeError: 'function' object has no attribute 'get_extra_actions' error in Python typically occurs when you mistakenly try to call a method on a function object instead of an instance of a class with the method.


Example 1: Misusing a Function Reference Instead of an Object


Codes Before fixing the AttributeError: 'function' object has no attribute 'get_extra_actions Python


# Misusing a Function Reference Instead of an Object

def my_function():
return "Hello Python"

# Incorrect usage
result = my_function.get_extra_actions()


In this example, my_function is a regular Python function. Functions do not have attributes or methods like get_extra_actions.


Output:




Solution:

Ensure that you are calling get_extra_actions on an object that has this method.


Codes After fixing the AttributeError: 'function' object has no attribute 'get_extra_actions Python


class MyClass:
def get_extra_actions(self):
return "Hello Python"


# Correct usage
obj = MyClass()
result = obj.get_extra_actions()
print(result)



Example 2: Misassigned Function Instead of a Class Instance


Codes Before fixing the AttributeError: 'function' object has no attribute 'get_extra_actions Python


# Misassigned Function Instead of a Class Instance
class MyView:
def get_extra_actions(self):
return ["action1", "action2"]

# Incorrect assignment
view_instance = MyView.get_extra_actions

# Trying to call a method on the function reference
actions = view_instance.get_extra_actions()


Here, MyView.get_extra_actions refers to the function itself, not an instance of MyView. When you try to call view_instance.get_extra_actions(), you're effectively trying to call a method on an invalid function.


Output:




Solution:

Instantiate the class before calling its method.


Codes After fixing the AttributeError: 'function' object has no attribute 'get_extra_actions Python



class MyView:
def get_extra_actions(self):
return ["action1", "action2"]

view_instance = MyView()
actions = view_instance.get_extra_actions()
print(actions)


Example 3: Overwriting a Method with a Function Reference


Codes Before fixing the AttributeError: 'function' object has no attribute 'get_extra_actions Python



# Overwriting a Method with a Function Reference
class MyClass:
def get_extra_actions(self):
return ["action1", "action2"]


# Creating an instance of MyClass
my_instance = MyClass()


# Overwriting the method with a function reference
def my_function():
return ["new_action1", "new_action2"]


my_instance.get_extra_actions = my_function

# Trying to access an attribute of the original method This will raise an AttributeError
my_instance.get_extra_actions.get_extra_actions()



Output:




To avoid this, you can rename the function or create a wrapper method that maintains the original method while allowing you to add custom behaviour. Here's how you can do it:


Solution 1: Use a Different Name for the Function


# Use a Different Name for the Function

class MyClass:
def get_extra_actions(self):
return ["action1", "action2"]

# Creating an instance of MyClass
my_instance = MyClass()

# Define a new function with a different name
def my_function():
return ["new_action1", "new_action2"]

# Accessing the new function directly
new_actions = my_function()
print(new_actions)

# Accessing the original method
original_actions = my_instance.get_extra_actions()
print(original_actions)


Solution 2: Use a Wrapper Method

If you want to keep the original method but add custom behaviour, you can define a wrapper function like this:


# Use a Wrapper Method
class MyClass:
def get_extra_actions(self):
return ["action1", "action2"]

# Creating an instance of MyClass
my_instance = MyClass()

# Define a wrapper function that calls the original method
def my_function():
original_actions = my_instance.get_extra_actions() # Call the original method
new_actions = ["new_action1", "new_action2"]
return original_actions + new_actions

# Assign the wrapper function to a new attribute if needed
my_instance.get_new_actions = my_function

# Call the original method
print(my_instance.get_extra_actions()) # Outputs: ["action1", "action2"]

# Call the wrapper method
print(my_instance.get_new_actions()) # Outputs: ["action1", "action2", "new_action1", "new_action2"]


Using a different name or a wrapper can avoid the AttributeError and keep the original functionality intact.


Conclusion

To fix or resolve the AttributeError: 'function' object has no attribute 'glob' and AttributeError: 'function' object has no attribute 'get_extra_actions', you normally need to carefully check how you invoke functions and methods in your Python code.


These errors raise from incorrect usage or misnamed variables that conflict with method names in your code.


Following the above examples can avoid these AttributeErrors.

Happy coding!!!


Other Posts:

1 With Examples Fix: Python Object Has No Attribute, but it Does.


2 With Examples Fix attributeerror: 'function' object has no attribute func and AttributeError: 'function' object has no attribute assert_called Python.


3 With Examples Fix: AttributeError: 'function' object has no attribute 'patch and Attributeerror: 'function' object has no attribute count Python


4 With Examples Fix: ModuleNotFoundError: No module named 'module' and ImportError: attempted relative import with no known parent package Python


5 With Examples Fix: attributeerror: 'function' object has no attribute register and AttributeError: 'function' object has no attribute 'get Python


6 How to Fix AttributeError: 'dict_values' object has no attribute 'update_relative' in Python With Examples.