Private methods

Discussion in 'Python' started by D.M. Procida, Oct 9, 2012.

  1. D.M. Procida

    D.M. Procida Guest

    What exactly is the point of a private method? Why or when would I want
    to use one?

    Daniele
     
    D.M. Procida, Oct 9, 2012
    #1
    1. Advertising

  2. Mark Lawrence, Oct 9, 2012
    #2
    1. Advertising

  3. D.M. Procida

    D.M. Procida Guest

    Mark Lawrence <> wrote:

    > On 09/10/2012 14:24, D.M. Procida wrote:
    > > What exactly is the point of a private method? Why or when would I want
    > > to use one?
    > >
    > > Daniele
    > >

    >
    > Hardly a Python question but using a search engine could have got you
    > here, and rather faster :)
    >

    http://stackoverflow.com/questions/2620699/why-private-methods-in-the-ob
    ject-oriented

    Thanks. Sometimes I prefer to talk to real people on Usenet than do web
    searches. Just my preference.

    Anyway, one of the answers on that page explains that public methods are
    interfaces to a class, that other things might rely on, and private ones
    are for its own internal logic, that other things don't need to care
    about.

    In Python, using an underscore is simply a convention to note that a
    method is private - it doesn't actually hide it from other things -
    correct?

    Daniele
     
    D.M. Procida, Oct 9, 2012
    #3
  4. On 12-10-09 06:59 AM, D.M. Procida wrote:
    > In Python, using an underscore is simply a convention to note that a
    > method is private - it doesn't actually hide it from other things -
    > correct?
    >
    > Daniele
    >


    A single underscore semantically means private. A double underscore will
    name mangle the function such that it's only accessible strictly by name
    through the class that it's define in. Note that you *can* still access
    it if you understand how name mangling works. Nothing in Python is truly
    private.

    --
    Demian Brecht
    @demianbrecht
    http://demianbrecht.github.com
     
    Demian Brecht, Oct 9, 2012
    #4
  5. D.M. Procida

    Robert Kern Guest

    On 10/9/12 2:59 PM, D.M. Procida wrote:
    > Mark Lawrence <> wrote:
    >
    >> On 09/10/2012 14:24, D.M. Procida wrote:
    >>> What exactly is the point of a private method? Why or when would I want
    >>> to use one?
    >>>
    >>> Daniele
    >>>

    >>
    >> Hardly a Python question but using a search engine could have got you
    >> here, and rather faster :)
    >>

    > http://stackoverflow.com/questions/2620699/why-private-methods-in-the-ob
    > ject-oriented
    >
    > Thanks. Sometimes I prefer to talk to real people on Usenet than do web
    > searches. Just my preference.


    That's understandable, but the real people on Usenet who will answer your
    questions usually prefer that you do a web search first, for a variety of reasons.

    http://www.catb.org/esr/faqs/smart-questions.html#before

    > Anyway, one of the answers on that page explains that public methods are
    > interfaces to a class, that other things might rely on, and private ones
    > are for its own internal logic, that other things don't need to care
    > about.
    >
    > In Python, using an underscore is simply a convention to note that a
    > method is private - it doesn't actually hide it from other things -
    > correct?


    This is correct.

    --
    Robert Kern

    "I have come to believe that the whole world is an enigma, a harmless enigma
    that is made terrible by our own mad attempt to interpret it as though it had
    an underlying truth."
    -- Umberto Eco
     
    Robert Kern, Oct 9, 2012
    #5
  6. D.M. Procida

    Tim Chase Guest

    On 10/09/12 08:59, D.M. Procida wrote:
    >> On 09/10/2012 14:24, D.M. Procida wrote:
    >>> What exactly is the point of a private method? Why or when would I want
    >>> to use one?

    >
    > In Python, using an underscore is simply a convention to note that a
    > method is private - it doesn't actually hide it from other things -
    > correct?


    Correct. You can read the leading underscore as "if you use this,
    don't come crying if it breaks on you in a future version because we
    changed an internal implementation. We told you so."

    -tkc
     
    Tim Chase, Oct 9, 2012
    #6
  7. D.M. Procida

    Ian Kelly Guest

    On Tue, Oct 9, 2012 at 8:08 AM, Demian Brecht <> wrote:
    > A single underscore semantically means private. A double underscore will
    > name mangle the function such that it's only accessible strictly by name
    > through the class that it's define in. Note that you *can* still access it
    > if you understand how name mangling works. Nothing in Python is truly
    > private.


    I tend to view name mangling as being more for avoiding internal
    attribute collisions in complex inheritance structures than for
    designating names as private.
     
    Ian Kelly, Oct 9, 2012
    #7
  8. On Tue, 09 Oct 2012 11:08:13 -0600, Ian Kelly wrote:

    > On Tue, Oct 9, 2012 at 8:08 AM, Demian Brecht <>
    > wrote:
    >> A single underscore semantically means private. A double underscore
    >> will name mangle the function such that it's only accessible strictly
    >> by name through the class that it's define in. Note that you *can*
    >> still access it if you understand how name mangling works. Nothing in
    >> Python is truly private.

    >
    > I tend to view name mangling as being more for avoiding internal
    > attribute collisions in complex inheritance structures than for
    > designating names as private.


    Really? I tend to view name mangling as a waste of time, and complex
    inheritance structures as something to avoid.



    --
    Steven
     
    Steven D'Aprano, Oct 10, 2012
    #8
  9. On 12-10-09 04:51 PM, Steven D'Aprano wrote:
    > Really? I tend to view name mangling as a waste of time, and complex
    > inheritance structures as something to avoid.


    Yep, I've been coming around to this as of late.

    --
    Demian Brecht
    @demianbrecht
    http://demianbrecht.github.com
     
    Demian Brecht, Oct 10, 2012
    #9
  10. D.M. Procida

    D.M. Procida Guest

    Demian Brecht <> wrote:

    > On 12-10-09 04:51 PM, Steven D'Aprano wrote:
    > > Really? I tend to view name mangling as a waste of time, and complex
    > > inheritance structures as something to avoid.

    >
    > Yep, I've been coming around to this as of late.


    I have a lot of inheritance. I don't know whether you'd call it complex,
    but I use a lot of mixins to build classes.

    It certainly makes it quick to build a class with the attributes I need,
    but it does make tracing logic sometimes a pain in the neck.

    I don't know what the alternative is though.

    Daniele
     
    D.M. Procida, Oct 10, 2012
    #10
  11. D.M. Procida

    Robert Kern Guest

    On 10/10/12 12:51 AM, Steven D'Aprano wrote:
    > On Tue, 09 Oct 2012 11:08:13 -0600, Ian Kelly wrote:
    >
    >> On Tue, Oct 9, 2012 at 8:08 AM, Demian Brecht <>
    >> wrote:
    >>> A single underscore semantically means private. A double underscore
    >>> will name mangle the function such that it's only accessible strictly
    >>> by name through the class that it's define in. Note that you *can*
    >>> still access it if you understand how name mangling works. Nothing in
    >>> Python is truly private.

    >>
    >> I tend to view name mangling as being more for avoiding internal
    >> attribute collisions in complex inheritance structures than for
    >> designating names as private.

    >
    > Really? I tend to view name mangling as a waste of time, and complex
    > inheritance structures as something to avoid.


    Whatever you may think of the use case, it was the motivating reason why it was
    put into the language:

    http://docs.python.org/reference/lexical_analysis.html#reserved-classes-of-identifiers

    --
    Robert Kern

    "I have come to believe that the whole world is an enigma, a harmless enigma
    that is made terrible by our own mad attempt to interpret it as though it had
    an underlying truth."
    -- Umberto Eco
     
    Robert Kern, Oct 10, 2012
    #11
  12. D.M. Procida

    Roy Smith Guest

    In article
    <1krpdak.u0qy9e1a4knspN%>,
    (D.M. Procida) wrote:

    > Mark Lawrence <> wrote:
    >
    > > On 09/10/2012 14:24, D.M. Procida wrote:
    > > > What exactly is the point of a private method? Why or when would I want
    > > > to use one?
    > > >
    > > > Daniele
    > > >

    > >
    > > Hardly a Python question but using a search engine could have got you
    > > here, and rather faster :)
    > >

    > http://stackoverflow.com/questions/2620699/why-private-methods-in-the-ob
    > ject-oriented
    >
    > Thanks. Sometimes I prefer to talk to real people on Usenet than do web
    > searches. Just my preference.
    >
    > Anyway, one of the answers on that page explains that public methods are
    > interfaces to a class, that other things might rely on, and private ones
    > are for its own internal logic, that other things don't need to care
    > about.
    >
    > In Python, using an underscore is simply a convention to note that a
    > method is private - it doesn't actually hide it from other things -
    > correct?


    Yes (modulo some details of how import works that I've never fully
    figured out and which lack of knowledge hasn't seemed to have hurt me
    any).

    I view public and private in Python this way:

    Public: I hereby declare that this method or attribute is part of the
    promised never to change interface of this class. I might possibly
    break that promise at some point in the future, but if I do, you have
    the right to bitch and whine about it, and I'm morally obligated to at
    least pretend I care.

    Private: I hereby declare that this method or attribute is something I
    needed to have for my own purposes, and is officially hidden inside my
    kimono. Like all things inside my kimono, you may speculate about their
    existence all you want, but you touch them at your own peril. I may
    change them at some point in the future, and while you can bitch and
    whine about it all you want, I'm not listening.
     
    Roy Smith, Oct 10, 2012
    #12
  13. On Wednesday, 10 October 2012 18:38:04 UTC+5:30, Roy Smith wrote:
    > In article
    >
    > <1krpdak.u0qy9e1a4knspN%>,
    >
    > (D.M. Procida) wrote:
    >
    >
    >
    > > Mark Lawrence <> wrote:

    >
    > >

    >
    > > > On 09/10/2012 14:24, D.M. Procida wrote:

    >
    > > > > What exactly is the point of a private method? Why or when would I want

    >
    > > > > to use one?

    >
    > > > >

    >
    > > > > Daniele

    >
    > > > >

    >
    > > >

    >
    > > > Hardly a Python question but using a search engine could have got you

    >
    > > > here, and rather faster :)

    >
    > > >

    >
    > > http://stackoverflow.com/questions/2620699/why-private-methods-in-the-ob

    >
    > > ject-oriented

    >
    > >

    >
    > > Thanks. Sometimes I prefer to talk to real people on Usenet than do web

    >
    > > searches. Just my preference.

    >
    > >

    >
    > > Anyway, one of the answers on that page explains that public methods are

    >
    > > interfaces to a class, that other things might rely on, and private ones

    >
    > > are for its own internal logic, that other things don't need to care

    >
    > > about.

    >
    > >

    >
    > > In Python, using an underscore is simply a convention to note that a

    >
    > > method is private - it doesn't actually hide it from other things -

    >
    > > correct?

    >
    >
    >
    > Yes (modulo some details of how import works that I've never fully
    >
    > figured out and which lack of knowledge hasn't seemed to have hurt me
    >
    > any).
    >
    >
    >
    > I view public and private in Python this way:
    >
    >
    >
    > Public: I hereby declare that this method or attribute is part of the
    >
    > promised never to change interface of this class. I might possibly
    >
    > break that promise at some point in the future, but if I do, you have
    >
    > the right to bitch and whine about it, and I'm morally obligated to at
    >
    > least pretend I care.
    >
    >
    >
    > Private: I hereby declare that this method or attribute is something I
    >
    > needed to have for my own purposes, and is officially hidden inside my
    >
    > kimono. Like all things inside my kimono, you may speculate about their
    >
    > existence all you want, but you touch them at your own peril. I may
    >
    > change them at some point in the future, and while you can bitch and
    >
    > whine about it all you want, I'm not listening.


    Uhum. Language please.
     
    Ramchandra Apte, Oct 10, 2012
    #13
  14. On 10/10/2012 16:56, Ramchandra Apte wrote:
    > On Wednesday, 10 October 2012 18:38:04 UTC+5:30, Roy Smith wrote:
    >> Public: I hereby declare that this method or attribute is part of the
    >> promised never to change interface of this class. I might possibly
    >> break that promise at some point in the future, but if I do, you have
    >> the right to bitch and whine about it, and I'm morally obligated to at
    >> least pretend I care.
    >>
    >> Private: I hereby declare that this method or attribute is something I
    >> needed to have for my own purposes, and is officially hidden inside my
    >> kimono. Like all things inside my kimono, you may speculate about their
    >> existence all you want, but you touch them at your own peril. I may
    >> change them at some point in the future, and while you can bitch and
    >> whine about it all you want, I'm not listening.

    >
    > Uhum. Language please.
    >


    What language? Further the original was readable, your use of CrapMail
    made life difficult until I stripped the superfluous newlines out. Is
    it really so awkward to equip yourself with a semi-decent mail reader?
    Like Thunderbird, hint, hint :)

    --
    Cheers.

    Mark Lawrence.
     
    Mark Lawrence, Oct 10, 2012
    #14
  15. On 2012-10-10, Mark Lawrence <> wrote:
    > On 10/10/2012 16:56, Ramchandra Apte wrote:
    >> On Wednesday, 10 October 2012 18:38:04 UTC+5:30, Roy Smith wrote:
    >>> Public: I hereby declare that this method or attribute is part of the
    >>> promised never to change interface of this class. I might possibly
    >>> break that promise at some point in the future, but if I do, you have
    >>> the right to bitch and whine about it, and I'm morally obligated to at
    >>> least pretend I care.
    >>>
    >>> Private: I hereby declare that this method or attribute is something I
    >>> needed to have for my own purposes, and is officially hidden inside my
    >>> kimono. Like all things inside my kimono, you may speculate about their
    >>> existence all you want, but you touch them at your own peril. I may
    >>> change them at some point in the future, and while you can bitch and
    >>> whine about it all you want, I'm not listening.

    >>
    >> Uhum. Language please.

    >
    > What language?


    Perhaps he didn't like the kimono metaphor?

    I always though the kimono metaphore as commonly used by MBA/sales
    types was a little unseemly (but then most of what those types say is,
    regardless of language or metaphor).

    > Further the original was readable, your use of CrapMail made life
    > difficult until I stripped the superfluous newlines out. Is it
    > really so awkward to equip yourself with a semi-decent mail reader?
    > Like Thunderbird, hint, hint :)


    You're tilting at windmills.

    Just give up and filter out all postings with a messageid ending in
    '@googlegroups.com'. I find that solves all sorts of problems...

    --
    Grant Edwards grant.b.edwards Yow! ! Now I understand
    at advanced MICROBIOLOGY and
    gmail.com th' new TAX REFORM laws!!
     
    Grant Edwards, Oct 10, 2012
    #15
  16. D.M. Procida

    alex23 Guest

    On 10 Oct, 17:03, (D.M.
    Procida) wrote:
    > It certainly makes it quick to build a class with the attributes I need,
    > but it does make tracing logic sometimes a pain in the neck.
    >
    > I don't know what the alternative is though.


    Components.

    The examples are in C++ and it's about game development, but I found
    this article to be very good at explaining the approach:
    http://gameprogrammingpatterns.com/component.html

    I've become a big fan of components & adaptation using zope.interface:
    http://wiki.zope.org/zope3/ZopeGuideComponents
     
    alex23, Oct 11, 2012
    #16
  17. D.M. Procida

    alex23 Guest

    On 11 Oct, 02:14, Mark Lawrence <> wrote:
    > What language?


    I think he's objecting to "bitch".

    I had to block him on G+ because he kept asking me to self-censor
    posts that he had _chosen to read_.
     
    alex23, Oct 11, 2012
    #17
  18. On Wed, 10 Oct 2012 18:34:01 -0700, alex23 wrote:

    > On 10 Oct, 17:03, (D.M.
    > Procida) wrote:
    >> It certainly makes it quick to build a class with the attributes I
    >> need, but it does make tracing logic sometimes a pain in the neck.
    >>
    >> I don't know what the alternative is though.

    >
    > Components.


    Composition. Delegation. Traits. Prototypes.

    Inheritance is great, but it is not a solution to everything.


    --
    Steven
     
    Steven D'Aprano, Oct 11, 2012
    #18
  19. D.M. Procidaæ–¼ 2012å¹´10月9日星期二UTC+8下åˆ9時24分30秒寫é“:
    > What exactly is the point of a private method? Why or when would I want
    >


    Private methods in the object level are searched first
    in the run time of python interpreter.

    I did turn some private methods in to c programs from time to time.
     
    88888 Dihedral, Oct 11, 2012
    #19
  20. alex23 <> writes:

    > On 10 Oct, 17:03, (D.M.
    > Procida) wrote:
    >> It certainly makes it quick to build a class with the attributes I need,
    >> but it does make tracing logic sometimes a pain in the neck.
    >>
    >> I don't know what the alternative is though.

    >
    > Components.
    >
    > The examples are in C++ and it's about game development, but I found
    > this article to be very good at explaining the approach:
    > http://gameprogrammingpatterns.com/component.html
    >
    > I've become a big fan of components & adaptation using zope.interface:
    > http://wiki.zope.org/zope3/ZopeGuideComponents


    If multiple inheritance is deemed complex, adaptation is even more so:

    With multiple inheritance, you can quite easily see from the source
    code how things are put together.
    Adaptation follows the "inversion of control" principle. With this
    principle, how a function is implemented, is decided outside
    and can very easily be changed (e.g. through configuration).
    This gives great flexibility but also nightmares when things do
    not work as expected...
     
    Dieter Maurer, Oct 11, 2012
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Buster Copley
    Replies:
    5
    Views:
    563
    Gianni Mariani
    Jul 7, 2003
  2. Daniel Finnie
    Replies:
    3
    Views:
    191
    Logan Capaldo
    Dec 16, 2006
  3. Frank Meyer

    Private methods not so private?

    Frank Meyer, Aug 1, 2007, in forum: Ruby
    Replies:
    14
    Views:
    261
  4. Kenneth McDonald
    Replies:
    5
    Views:
    329
    Kenneth McDonald
    Sep 26, 2008
  5. Robert
    Replies:
    16
    Views:
    195
Loading...

Share This Page