0

how can we not expose methods in Python and make them private as in Java?

specifically, my scenario involves functions that the user should not be using.

iKlsR
  • 2,642
  • 6
  • 27
  • 45
Neil
  • 459
  • 2
  • 6
  • 16
  • 6
    In python, things the user shouldn't be doing are usually *documented* as such. If the user really wants to misuse your library, that's his/her right (the philosophy is a bit different from Java). – akaIDIOT Mar 22 '13 at 14:08
  • Basically you can't. Python has conventions for marking things "private", but if another programmer wants to use them, there's no way to stop them. – GreenMatt Mar 22 '13 at 14:11
  • ok, we cannot do that...but why the -1? – Neil Mar 22 '13 at 15:50

1 Answers1

1

I think the only way to make inaccessible methods is like this

class A:
   def some_func(self,*some_Args):
       def this_is_innaccessible_function():
           return  "yellow"

       print this_is_innaccessible_function()

however it is also inaccessible to the rest of the class... the only place it is accessible is within some_func

standard convention tells us to mark private funcs with double underscores, this causes some name mangling behind the scenes which makes it marginally more difficult to access from outside the class

class A:
    def __private_by_convention(self):
        print "this should not be called outside of class...but it can be"
Joran Beasley
  • 110,522
  • 12
  • 160
  • 179