With or without leading underscore...
Carl Banks
pavlovevidence at gmail.com
Mon Aug 10 13:28:25 EDT 2009
On Aug 10, 7:37 am, Ulrich Eckhardt <eckha... at satorlaser.com> 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
More information about the Python-list
mailing list