Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python attributeError on __del__

I have a python class object and I want to assign the value of one class variable

class Groupclass(Workerclass):
    """worker class"""
    count = 0

    def __init__(self):
        """initialize time"""
        Groupclass.count += 1
        self.membercount = 0;
        self.members = []

    def __del__(self):
        """delte a worker data"""
        Groupclass.count -= 1


if __name__ == "__main__":
    group1 = Groupclass()

This execution result is correct, but there's an error message that says:

Exception AttributeError: "'NoneType' object has no attribute 'count'" in <bound method Groupclass.__del__ of <__main__.Groupclass instance at 0x00BA6710>> ignored

Can someone tell me what me I did wrong?

like image 416
Shih-Min Lee Avatar asked Aug 05 '13 12:08

Shih-Min Lee


People also ask

What is __ del __ in Python?

__del__ is a destructor method which is called as soon as all references of the object are deleted i.e when an object is garbage collected. Syntax: def __del__(self): body of destructor . . Example: Here is the simple example of destructor.

How do I fix AttributeError in Python?

Solution for AttributeError Errors and exceptions in Python can be handled using exception handling i.e. by using try and except in Python. Example: Consider the above class example, we want to do something else rather than printing the traceback Whenever an AttributeError is raised.

How do you use Hasattr in Python?

Python hasattr() FunctionThe hasattr() function returns True if the specified object has the specified attribute, otherwise False .

What is __ Setattr __?

Python's magic method __setattr__() implements the built-in setattr() function that takes an object and an attribute name as arguments and removes the attribute from the object. We call this a “Dunder Method” for “Double Underscore Method” (also called “magic method”).


2 Answers

Your __del__ method assumes that the class is still present by the time it is called.

This assumption is incorrect. Groupclass has already been cleared when your Python program exits and is now set to None.

Test if the global reference to the class still exists first:

def __del__(self):
    if Groupclass:
        Groupclass.count -= 1

or use type() to get the local reference:

def __del__(self):
    type(self).count -= 1

but do note that this means that the semantics for count change if Groupclass is subclassed (each subclass gets a .count attribute versus only Groupclass having a .count attribute).

Quoting from the __del__ hook documentation:

Warning: Due to the precarious circumstances under which __del__() methods are invoked, exceptions that occur during their execution are ignored, and a warning is printed to sys.stderr instead. Also, when __del__() is invoked in response to a module being deleted (e.g., when execution of the program is done), other globals referenced by the __del__() method may already have been deleted or in the process of being torn down (e.g. the import machinery shutting down). For this reason, __del__() methods should do the absolute minimum needed to maintain external invariants. Starting with version 1.5, Python guarantees that globals whose name begins with a single underscore are deleted from their module before other globals are deleted; if no other references to such globals exist, this may help in assuring that imported modules are still available at the time when the __del__() method is called.

If you are using Python 3, two additional notes apply:

  • CPython 3.3 automatically applies a randomized hash salt to the str keys used in a globals dictionary; this also affects the order in which globals are cleaned up, and it could be that you see the problem on only some of the runs.

  • CPython 3.4 no longer sets globals to None (in most cases), as per Safe Object Finalization; see PEP 442.

like image 188
Martijn Pieters Avatar answered Oct 24 '22 10:10

Martijn Pieters


When __del__() method called, the Groupclass may be recovered by the garbage collection mechanism, so using Groupclass.xxx may failed. But you can access the count variable through self.__class__.count. Code likes below:

def __del__(self):
    self.__class__.count -= 1
like image 1
eraul Avatar answered Oct 24 '22 11:10

eraul