On Apr 25, 2007, at 10:10 AM, johnf wrote:

> Since you took the time to explain scope and namespace could you  
> explain "__"
> in context of Dabo.  Why is it necessary to mangle functions?

        The double-under is a convention that allows you to create a name  
that will not be overridden by a subclass or instance. Python  
accomplishes this internally by translating MyClass.__foo to  
MyClass._MyClass__foo at runtime. So imagine that you have this class  
structure:

class TheSuper(object):
        def __foo(self):
                print "Super Class"

class TheSub(TheSuper):
        def __foo(self):
                print "Sub Class"

obj = TheSub()

        At this point, obj does *not* have a __foo() method, even though it  
is defined in both of its superclasses. It does have a _TheSub__foo()  
and a _TheSuper__foo() method, though.

        While this is often cited as a way of creating private members, it's  
actually not designed for that at all; its main purpose is to provide  
protected members that cannot be accidentally overridden by a subclass.

-- Ed Leafe
-- http://leafe.com
-- http://dabodev.com




_______________________________________________
Post Messages to: [email protected]
Subscription Maintenance: http://leafe.com/mailman/listinfo/dabo-users
Searchable Archives: http://leafe.com/archives/search/dabo-users
This message: http://leafe.com/archives/byMID/dabo-users/[EMAIL PROTECTED]

Reply via email to