Python error creating an instance of an object inside another

I create a very simple container object in python, and one of its functions requires the creation of a temporary null placeholder class that does absolutely nothing but tell the program what to delete.

class __cyclepass(object):
    "Null class as a placeholder for deleting items in a cycle."""
    pass

class Cycle(object):

    def __init__(self, *args):
        self.l = list(args)

    #.........................

    def __delitem__(self, key):
        """Magic method for deleting items via indexing."""
        try:
            if isinstance(key, slice):
                s = key.start; e = key.stop; stp = key.step if key.step else 1
                slicelen = abs((s - e) // stp)
                if slicelen == 1:
                    steps = [s]
                else:
                    steps = [s + stp * i for i in range(slicelen)]
                _list = copy(self.l)
                for step in steps:
                    index = step % len(self)
                    _list[index] = __cyclepass() #this is where an error occurs
                self.l = list(filter(lambda x: x.__class__ != __cyclepass, _list))
            else:
                index = key % len(self)
                del self.l[index]
        except:
            raise IndexError("Bad index %s" % str(key))

Everything seems fine (although a little messy, but it’s not), but after starting the program and implicitly calling the delitem method , I get this error:

NameError: global name '_Cycle__cyclepass' is not defined

What could be the reason for the search _Cycle__cyclepasswhen creating an object __cyclepass?

+4
source share
1 answer

[translation of my comment on the answer as suggested]

python name mangling "private". , .

+5

All Articles