Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > With or without leading underscore...

Reply
Thread Tools

With or without leading underscore...

 
 
Ulrich Eckhardt
Guest
Posts: n/a
 
      08-10-2009
....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

 
Reply With Quote
 
 
 
 
Jan Kaliszewski
Guest
Posts: n/a
 
      08-10-2009
10-08-2009 Ulrich Eckhardt <(E-Mail Removed)> 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) <(E-Mail Removed)>
 
Reply With Quote
 
 
 
 
Carl Banks
Guest
Posts: n/a
 
      08-10-2009
On Aug 10, 7:37*am, Ulrich Eckhardt <(E-Mail Removed)> 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
 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      08-11-2009
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
 
Reply With Quote
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      08-11-2009
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 ?-)
 
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
Emit YAML without leading hyphens? bwv549 Ruby 8 04-10-2009 02:41 PM
All leading tabs or all leading spaces - why isn't that enforced? John Nagle Python 4 08-07-2007 04:05 PM
RE: All leading tabs or all leading spaces - why isn't that enforced? Delaney, Timothy (Tim) Python 0 08-07-2007 03:50 AM
Hexadecimal formatting with leading zeros?! Dmitry Bond. ASP .Net 1 10-13-2005 08:55 AM
Formatting date without leading zeros Tom Reinhart Ruby 10 05-15-2005 06:51 PM



Advertisments