LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-24-2014, 10:27 AM   #1
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
Python overriding inherited private methods


Hi,

I was playing around with python and noticed something:

Code:
#!/usr/bin/env python3

class PrivateParent:
    def do_something(self):
        self.__print_message()

    def __print_message(self):
        print("In Parent!")

class PrivateChild(PrivateParent):
    def __print_message(self):
        print("In Child!")

foo = PrivateChild()
foo.do_something()
will print out "In Parent!", while

Code:
#!/usr/bin/env python3

class PublicParent:
    def do_something(self):
        self.print_message()

    def print_message(self):
        print("In Parent!")

class PublicChild(PublicParent):
    def print_message(self):
        print("In Child!")

foo = PublicChild()
foo.do_something()
will print out "In Child!".

I want the second behaviour, but it doesn't really make sense to expose the method in question publicly. Is there any way around this?

Additionally, does anyone have a good explanation for why this is?

Thanks,
 
Old 09-24-2014, 10:41 AM   #2
mina86
Member
 
Registered: Aug 2008
Distribution: Debian
Posts: 517

Rep: Reputation: 229Reputation: 229Reputation: 229
__foo identifiers are mangled to _ClassName__foo form so __print_message is _PrivateParent__print_message in PrivateParent class and _PrivateChild__print_message in PrivateChild class.
Code:
class PrivateParent:
    def do_something(self):
        self.__print_message()

    def __print_message(self):
        print("In Parent!")

class PrivateChild(PrivateParent):
    def _PrivateParent__print_message(self):
        print("In Child!")

foo = PrivateChild()
foo.do_something()
You should, of course, try to avoid doing this. Trying to access private members of other classes may indicate fault in your design. If the data/method is meant to be accessed from children, it should be protected (or public). If it's not meant to be accessed, child should not touch it.
 
1 members found this post helpful.
Old 09-24-2014, 10:47 AM   #3
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632

Original Poster
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
Ah, I knew they were mangled but couldn't remember exactly how.

Hm, I guess you're right that if the child is overriding, then the method "should" be public, hadn't really thought of it that way.

Thanks!
 
Old 09-24-2014, 11:42 AM   #4
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,224

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
Quote:
Originally Posted by mina86 View Post
If the data/method is meant to be accessed from children, it should be protected (or public).
Python has no concept of "protected", but starting identifier names with single underscores is a common way to signify it.
 
Old 09-24-2014, 01:46 PM   #5
mina86
Member
 
Registered: Aug 2008
Distribution: Debian
Posts: 517

Rep: Reputation: 229Reputation: 229Reputation: 229
Quote:
Originally Posted by dugan View Post
Python has no concept of "protected", but starting identifier names with single underscores is a common way to signify it.
Sure, if you want to be anal about it, Python has no concept of “private” either, but starting identifier name with two underscores mangles it.
 
  


Reply

Tags
inheritance, python3



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Using Open Source Methods in a Private Company LXer Syndicated Linux News 0 07-31-2013 06:00 PM
[SOLVED] C++ violating encapsulation (access to private vars without methods) Netooo Programming 1 05-28-2012 10:19 PM
No supported authentication methods available when using a private key for login j.smith1981 Linux - Server 8 03-02-2011 08:14 AM
Overriding inherited member functions in C++ vtable? disruptive Programming 14 08-18-2010 06:52 AM
Overloading methods in Python? xaoc Programming 0 10-28-2003 10:03 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:50 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration