Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Private methods

Reply
Thread Tools

Private methods

 
 
D.M. Procida
Guest
Posts: n/a
 
      10-09-2012
What exactly is the point of a private method? Why or when would I want
to use one?

Daniele
 
Reply With Quote
 
 
 
 
Mark Lawrence
Guest
Posts: n/a
 
      10-09-2012
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/2...bject-oriented

--
Cheers.

Mark Lawrence.

 
Reply With Quote
 
 
 
 
D.M. Procida
Guest
Posts: n/a
 
      10-09-2012
Mark Lawrence <(E-Mail Removed)> 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/2...hods-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
 
Reply With Quote
 
Demian Brecht
Guest
Posts: n/a
 
      10-09-2012
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
 
Reply With Quote
 
Robert Kern
Guest
Posts: n/a
 
      10-09-2012
On 10/9/12 2:59 PM, D.M. Procida wrote:
> Mark Lawrence <(E-Mail Removed)> 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/2...hods-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-q...ns.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

 
Reply With Quote
 
Tim Chase
Guest
Posts: n/a
 
      10-09-2012
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



 
Reply With Quote
 
Ian Kelly
Guest
Posts: n/a
 
      10-09-2012
On Tue, Oct 9, 2012 at 8:08 AM, Demian Brecht <(E-Mail Removed)> 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.
 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      10-09-2012
On Tue, 09 Oct 2012 11:08:13 -0600, Ian Kelly wrote:

> On Tue, Oct 9, 2012 at 8:08 AM, Demian Brecht <(E-Mail Removed)>
> 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
 
Reply With Quote
 
Demian Brecht
Guest
Posts: n/a
 
      10-10-2012
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
 
Reply With Quote
 
D.M. Procida
Guest
Posts: n/a
 
      10-10-2012
Demian Brecht <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there a way to find the class methods of a class, just like'methods' finds the instance methods? Kenneth McDonald Ruby 5 09-26-2008 03:09 PM
Private methods not so private? Frank Meyer Ruby 14 08-02-2007 07:29 PM
Why is define_method private? Plus,what's the point of private methods? Daniel Finnie Ruby 3 12-16-2006 10:09 PM
Private methods called from public methods Robert Javascript 16 05-06-2006 11:32 PM
Re: Templates and friends and template methods with private methods. Buster Copley C++ 5 07-07-2003 12:50 AM



Advertisments