Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8524813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T07:47:40+00:00 2026-06-11T07:47:40+00:00

Class Bar inherits from Foo: class Foo(object): def foo_meth_1(self): return ‘foometh1’ def foo_meth_2(self): return

  • 0

Class Bar inherits from Foo:

class Foo(object):
      def foo_meth_1(self):
          return 'foometh1'
      def foo_meth_2(self):
          return 'foometh2'
class Bar(Foo):
      def bar_meth(self):
          return 'bar_meth'

Is there a way of turning all methods inherited from Foo private?

class Bar(Foo):
      def bar_meth(self):
          return 'bar_meth'
      def __foo_meth_1(self):
          return 'foometh1'
      def __foo_meth_2(self):
          return 'foometh2'
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T07:47:41+00:00Added an answer on June 11, 2026 at 7:47 am

    Python doesn’t have privates, only obfuscated method names. But I suppose you could iterate over the methods of the superclass when creating the instance, removing them from yourself and creating new obfuscatingly named method names for those functions. setattr and getattr could be useful if you use a function to create obfuscated names.

    With that said, it’s a pretty cthuhlu-oid thing to do. You mention the intent is to keep the namespace cleaner, but this is more like mixing ammonia and chlorine. If the method needs to be hidden, hide it in the superclass. The don’t create instances of the superclass — instead create a specific class that wraps the hidden methods in public ones, which you could name the same thing but strip the leading whitespace.

    Assuming I understand your intent correctly, I would suggest doing something like this:

    class BaseFoo(object):
        def __init__(self):
            raise NotImplementedError('No instances of BaseFoo please.')
        def _foo(self):
            return 'Foo.'
        def _bar(self):
            return 'Bar.'
    
    class HiddenFoo(BaseFoo):
        def __init__(self): pass
    
    class PublicFoo(BaseFoo):
        def __init__(self): pass
        foo = BaseFoo._foo
        bar = BaseFoo._bar
    
    def try_foobar(instance):
        print 'Trying ' + instance.__class__.__name__
        try:
            print 'foo: ' + instance.foo
            print 'bar: ' + instance.bar
        except AttributeError, e:
            print e
    
    foo_1 = HiddenFoo()
    foo_2 = PublicFoo()
    try_foobar(foo_1)
    try_foobar(foo_2)
    

    And if PublicFoo.foo would do something more than BaseFoo.foo, you would write a wrapper that does whatever is needed, and then calls foo from the superclass.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following structure: class foo(object): class bar(object): def __init__(self, parent): self._parent=parent #this
class Liquid(object): def foo(self): pass def bar(self): pass class Water(Liquid): Say, I have the
Suppose I have a class Baz that inherits from classes Foo and Bar ,
In C#, I have a class, foo , that inherits from class bar ,
I have class Foo which defines property Id . Class Bar inherits from Foo
I'm confused about this scope behavior: class Bar: def __init__(self): for fn in [open,openW,remove,mkdir,exists,isdir,listdir]:
Here is my class Bar: class Bar: def __init__(self, start, end, open, volume, high=open,
I have: class Foo; class Bar { Foo foo; Bar(): foo(foo) {}; } Bar
Imagine I have a C++ class Foo and a class Bar which has to
Suppose I have: class Foo { ... }; class Bar : public Foo {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.