With or without leading underscore...

U

Ulrich Eckhardt

....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
 
J

Jan Kaliszewski

10-08-2009 Ulrich Eckhardt said:
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
 
C

Carl Banks

...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
 
S

Steven D'Aprano

...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.
 
B

Bruno Desthuilliers

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 ?-)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top