Reputation: 5074
I'm creating a very simple container object in python and one of it's functions requires to create a temporary, null placeholder class that does absolutely nothing except 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 (albeit a little bit messy but that is aside the point), but upon running the program and implicitly calling the delitem method I get this error:
NameError: global name '_Cycle__cyclepass' is not defined
What on earth would be causing it to look for _Cycle__cyclepass
when creating the __cyclepass
object?
Upvotes: 4
Views: 80
Reputation: 64318
[moving my comment to an answer, as suggested]
This is the result of python's name mangling of "private" members. Rename from a double-underscore prefix to single-underscore, and problem is solved.
Upvotes: 5