Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Here is my program structure:

class parent(object):
    def __init__(self): pass

    def __post_init_stuff(self):
       #post initialization stuff

class child(parent):
    def __init__(self):
       #dostuff
       self.__post_init_stuff()

def otherfunc(classname, args):
    classname(args)

otherfunc(child)

The problem that I'm having is that when otherstuff(child) executes, I get this error:

AttributeError: 'child' object has no attribute '_child__post_init_stuff'

Any advice?

share|improve this question
    
You haven't posted any code for otherstuff –  kindall 19 hours ago
3  
That is happening due to Name mangling. –  Ashwini Chaudhary 19 hours ago
2  
Also I will add that having post_init_stuff is a very bad idea. __init is already the generic function for stuff that happens, well, at init. If you need to expand that in child then you should call the partens init and then continue with child specific instructions. –  Puciek 19 hours ago
1  
Why are you using double underscores in the first place if you don't want mangled, private-even-from-subclasses names? Literally the only reason to ever use them is to cause exactly this scenario. –  abarnert 19 hours ago
    
@kindall It was a typo. I edited the question to reflect this –  Namrop 3 hours ago

1 Answer 1

up vote 0 down vote accepted

Names that start with __ in Python are like protected in C++. You could name with only one underscore instead or access it as self._parent__post_init_func, since this is its mangled name within the scope. I recommend the first thing, since this is ugly and hacky.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.