1

I have a class definition with two methods defined in it. The layout somewhat looks like below:

class Sample:
    def calling-Method(self):
        print "Hi"
        calledMethod()

    def called-Method(self):
        print "How are you"

I want called-Mehtod(self) should not be called from outside the class. The following should NOT be possible :

if __name__ == "__main__":
   obj = Sample()
   obj.called-Method()    #This should not allowed.

I researched and found out that python is not meant for privacy. Alternative is to use double underscore ("__") but this is not for privacy.

Is there any way to exhibit privacy in above scenario ? Any help is highly appreciated..

node_analyser
  • 1,502
  • 3
  • 17
  • 34
  • Check out this similar query: http://stackoverflow.com/questions/1547145/defining-private-module-functions-in-python – sshashank124 Jun 30 '14 at 06:10
  • There is no sane way to hide the methods from the caller. Why should you need something like this? Just document that the `called-method` should not be called outside the class. The leading `__` is mostly enough documentation. Also avoid the dash in the method name, please use an underscore, `called_method` and `calling_method`. – msvalkon Jun 30 '14 at 06:10
  • @msvalkon - It's nothing like "hiding" something from caller. I am just trying to understand the concepts of python's OOP implementations :) And Yes you are right, I will use underscore b/w method names. – node_analyser Jun 30 '14 at 06:18
  • Probably news for you: private methods have *nothing* to do with OO. Information hiding doesn't mean to prevent people from looking inside the objects. It simply means to provide the means to make that unneccessary while reducing the dependency on the implementation. The standard way to document a method as private is to simply give it a name that starts with *one* underscore. – Bakuriu Jun 30 '14 at 06:21
  • @v1h5 look at the link in my answer. There is an explanation of python's oop concept. – hnzlmnn Jun 30 '14 at 06:21
  • @sshashank124 - I read that question and answers related to it. This simply conveys that no matter what, the "private" part is just an illusion in python ? – node_analyser Jun 30 '14 at 06:28
  • I realize @BrenBarn has closed this question. However you may find this useful (*even if unconventional*): https://gist.github.com/therealprologic/4d05da7480b11ccd57a1 -- I vote to reopen this question as my gist here directly answers the question. – James Mills Jun 30 '14 at 06:35
  • Hi @JamesMills - I really appreciate this. Thank you. I am having a look at this right now. :) – node_analyser Jun 30 '14 at 06:39
  • @JamesMills: That is not a real solution. It will not work in methods that don't have `self` (such as classmethods or methods that choose ot name `self` something else), and it will not work (i.e., will allow access) in any function if that function just defines a variable called `self` pointing to the instance. In other words, it hides too much and yet not enough. – BrenBarn Jun 30 '14 at 06:50
  • @BrenBarn - I don't have in-depth knowledge of python as I am new to it but don't you think so that James's solution is like "something is better than nothing". Infact, I feel like python should provide something like James has shown in his git repository. Currently its solving my problem like a gem. – node_analyser Jun 30 '14 at 07:08
  • @v1h5: What is the problem you're trying to solve with this technique? – BrenBarn Jun 30 '14 at 07:13
  • @BrenBarn - I think I have conveyed about my problem. I want a method that should be only called from another methods of same class but that method should not be accessible from outside the class. – node_analyser Jun 30 '14 at 07:41
  • @v1h5 I will update my solution when I get home from work per BrenBarn comments so its more robust. – James Mills Jun 30 '14 at 07:48
  • @v1h5: The thing is, that's not a problem, it's a solution (or an attempt at one). What is it you're tyring to *do* that makes you think you need such a method? – BrenBarn Jun 30 '14 at 07:55
  • yes @JamesMills, you put it as answer. I was about to tell you. – node_analyser Jun 30 '14 at 17:53

1 Answers1

2

Python doesn't support access limitation as you know it from other oo languages. See here for a longer explanation.

Community
  • 1
  • 1
hnzlmnn
  • 393
  • 2
  • 14