-4

I have been trying to learn programming with python 3 for a couple of months now and I have gotten stuck on classes. Specifically private and public methods.

What I don't understand is what private methods are for. I understand that public methods can be directly accessed by the user / program. But I fail to understand private methods, what they are for and why they exist.

I did a good bit of research on Stackoverflow but didn't find anything to answer my question. So if this is a duplicate I apologize in advance.

vaultah
  • 44,105
  • 12
  • 114
  • 143
Steve T
  • 113
  • 6
  • Private methods do not exist in python – eyllanesc Apr 06 '17 at 21:19
  • What? I am going to laugh my self silly about this. The books I am using talk about public and private methods, but they don't exist? – Steve T Apr 06 '17 at 21:21
  • 5
    They exist only as a convention. If you name something with a leading underscore (eg: `def _foo(self, ...)`), most python programmers consider those to be private. They aren't actually, but by convention we treat them as if they were. – Bryan Oakley Apr 06 '17 at 21:22
  • In the object-oriented programming paradigm we talk about private methods, but it is up to each language to adopt those characteristics. – eyllanesc Apr 06 '17 at 21:22
  • @eyllanesc after doing some short research into the existence of private methods in python I found this [link](http://stackoverflow.com/questions/70528/why-are-pythons-private-methods-not-actually-private). And you are correct. They don't exist in python. Not as one would expect them to. – Steve T Apr 06 '17 at 21:26

2 Answers2

7

When you create objects, you are creating an API. You are giving that object attributes and methods that the outside world can interact with. car.drive(), horse.color, etc.

In the real world, the implementation of a class requires that you create a lot of methods specifically to make the class work, and are not part of this exposed API. Maybe you need a method called "honk", but you don't want it to be used anywhere except within your own code.

It is these internal methods that we consider to be "private". Some programming languages have formal constructs for making public or private methods. Python does not. However, by convention, methods and attributes that begin with an underscore are to be treated as private.

Just about every experienced python programmer, when seeing a method with an underscore, knows not to call it from anywhere outside of the class definition. They can, but they normally don't.

Why would you want some things to be private? One reason is that code changes. You add parameters, you remove parameters, you change algorithms. The public interface is something that doesn't normally change much once it's created. You may add to it, but you don't usually make big changes.

Private methods, by nature of being "hands off" to the outside world, can be freely changed without worrying about what other parts of the code may be calling those functions.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • Too bad I can't accept two answers. This one also explains what a private method does very well. Thank you as well @Bryan Oakley – Steve T Apr 06 '17 at 21:34
  • @SteveT You can always up-vote an answer. The answer you select is simply the one which helped you the most. But you can also up-vote other answers to show that they are good. – Christian Dean Apr 06 '17 at 21:40
  • @SteveT Yes. I forgot to mention that I was assuming you had enough reputation points. Don't sweat it. You'll have enough pretty soon. – Christian Dean Apr 06 '17 at 21:46
1

Private methods are intended for use by only your own class and not to be called anywhere else. The purpose it serves is encapsulation.

OpenUserX03
  • 1,448
  • 2
  • 14
  • 21
  • Now that makes sense. Why that wasn't explained in the books I am using confounds me. Thank you very much! – Steve T Apr 06 '17 at 21:23