Python strange override behaviour for "__" functions -



Python strange override behaviour for "__" functions -

i found "strange" behaviour in python overriding "__" functions

class a(object): def foo1(self): print "foo1 a" self.test1() def foo2(self): print "foo2 a" self.__test2() def test1(self): print "test1 a" def __test2(self): print "test2 a" class b(a): def test1(self): print "test1 b" def __test2(self): print "test2 b" ia = a() ib = b() ib.foo1() ib.foo2()

gives result:

foo1 test1 b foo2 test2

instead of:

foo1 test1 b foo2 test2 b

is normal behaviour python "__" functions?

the behaviour see stated intent of using leading double underscore name in class attribute or method.

names leading double underscore 'mangled'; have name of class prefixed, explicitly prevent name clashes subclasses.

see reserved classes of identifiers in reference documentation:

__* class-private names. names in category, when used within context of class definition, re-written utilize mangled form to help avoid name clashes between “private” attributes of base of operations , derived classes.

(emphasis mine)

also see identifiers section of expressions documentation:

private name mangling: when identifier textually occurs in class definition begins 2 or more underscore characters , not end in 2 or more underscores, considered private name of class. private names transformed longer form before code generated them. transformation inserts class name, leading underscores removed , single underscore inserted, in front end of name. example, identifier __spam occurring in class named ham transformed _ham__spam. transformation independent of syntactical context in identifier used. if transformed name extremely long (longer 255 characters), implementation defined truncation may happen. if class name consists of underscores, no transformation done.

python function private

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -