With or without leading underscore...

Discussion in 'Python' started by Ulrich Eckhardt, Aug 10, 2009.

  1. ....that is the question!

    I have a module which exports a type. It also exports a function that
    returns instances of that type. Now, the reason for my question is that
    while users will directly use instances of the type, they will not create
    instances of the type themselves.

    So, the type is a part of the public API, but its constructor is not. Should
    I mark the type as private (with a leading underscore) or not?

    Thanks!

    Uli

    --
    Sator Laser GmbH
    Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932
    Ulrich Eckhardt, Aug 10, 2009
    #1
    1. Advertising

  2. 10-08-2009 Ulrich Eckhardt <> wrote:

    > So, the type is a part of the public API, but its constructor is not.
    > Should
    > I mark the type as private (with a leading underscore) or not?


    IMHO you shouldn't (i.e. name should be marked "public") because of
    possible usage of e.g. "isinstance(foo, YourType)".

    Cheers,
    *j

    --
    Jan Kaliszewski (zuo) <>
    Jan Kaliszewski, Aug 10, 2009
    #2
    1. Advertising

  3. Ulrich Eckhardt

    Carl Banks Guest

    On Aug 10, 7:37 am, Ulrich Eckhardt <> wrote:
    > ...that is the question!
    >
    > I have a module which exports a type. It also exports a function that
    > returns instances of that type. Now, the reason for my question is that
    > while users will directly use instances of the type, they will not create
    > instances of the type themselves.
    >
    > So, the type is a part of the public API, but its constructor is not. Should
    > I mark the type as private (with a leading underscore) or not?



    I would not use the underscore.

    If the initializer is private you might consider dispensing with
    __init__ (have it raise TypeError), and initialize it from a "private"
    method or classmethod, or even directly from the factory function.
    And if you're wondering how the hell you're going to create the object
    of type A when you've disabled __init__, see the example classmethod
    below.


    class A(object):
    def __init__(self,*args,**kwargs):
    raise TypeError('Type not callable; use factory function
    instead')

    @classmethod
    def _create_object(cls,initial_value):
    self = object.__new__(cls) # avoid __init__
    self.value = initial_value



    Carl Banks
    Carl Banks, Aug 10, 2009
    #3
  4. On Mon, 10 Aug 2009 16:37:25 +0200, Ulrich Eckhardt wrote:

    > ...that is the question!
    >
    > I have a module which exports a type. It also exports a function that
    > returns instances of that type. Now, the reason for my question is that
    > while users will directly use instances of the type, they will not
    > create instances of the type themselves.
    >
    > So, the type is a part of the public API, but its constructor is not.
    > Should I mark the type as private (with a leading underscore) or not?


    My opinion is that if you have to ask the question "Should this class be
    private?", then the answer is No.

    Only make objects private if you have specific reasons for doing so. I
    know this goes against the advice given by other languages (namely, make
    everything private unless you need it to be public) but Python encourages
    openness and transparency.



    --
    Steven
    Steven D'Aprano, Aug 11, 2009
    #4
  5. Carl Banks a écrit :
    (snip)


    > class A(object):
    > def __init__(self,*args,**kwargs):
    > raise TypeError('Type not callable; use factory function
    > instead')
    >
    > @classmethod
    > def _create_object(cls,initial_value):
    > self = object.__new__(cls) # avoid __init__
    > self.value = initial_value


    I assume there's a missing "return self" line here ?-)
    Bruno Desthuilliers, Aug 11, 2009
    #5
    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. Walter Tross

    leading underscore for member names

    Walter Tross, Apr 29, 2004, in forum: C++
    Replies:
    5
    Views:
    534
    Sumit Rajan
    Apr 29, 2004
  2. Axter

    leading underscore

    Axter, Nov 11, 2005, in forum: C++
    Replies:
    10
    Views:
    552
  3. Pete C
    Replies:
    5
    Views:
    407
    =?ISO-8859-15?Q?Juli=E1n?= Albo
    Dec 14, 2006
  4. John Nagle
    Replies:
    4
    Views:
    580
    James Antill
    Aug 7, 2007
  5. Delaney, Timothy (Tim)
    Replies:
    0
    Views:
    559
    Delaney, Timothy (Tim)
    Aug 7, 2007
Loading...

Share This Page