Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > naming convention for scalars, lists, dictionaries ...

Reply
Thread Tools

naming convention for scalars, lists, dictionaries ...

 
 
beliavsky@aol.com
Guest
Posts: n/a
 
      02-28-2005
Since Python does not have declarations, I wonder if people think it is
good to name function arguments according to the type of data structure
expected, with names like "xlist" or "xdict".

 
Reply With Quote
 
 
 
 
Skip Montanaro
Guest
Posts: n/a
 
      02-28-2005

beliavsky> Since Python does not have declarations, I wonder if people
beliavsky> think it is good to name function arguments according to the
beliavsky> type of data structure expected, with names like "xlist" or
beliavsky> "xdict".

In general, no. I think variable names should reflect what they are naming,
not their types.

Skip
 
Reply With Quote
 
 
 
 
Steven Bethard
Guest
Posts: n/a
 
      02-28-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Since Python does not have declarations, I wonder if people think it is
> good to name function arguments according to the type of data structure
> expected, with names like "xlist" or "xdict".


In general, I find that naming collections for their contents is much
more useful than some abbreviated type prefix. However, while I don't
name objects by their type, I do tend to name iterables with plurals
(e.g. "words", "feature_indices", "events", etc.) and I typically suffix
mapping types with "map" (e.g. "word_index_map", "event_relation_map",
"prime_factor_map", etc.)

STeVe
 
Reply With Quote
 
Jack Diederich
Guest
Posts: n/a
 
      02-28-2005
On Mon, Feb 28, 2005 at 11:32:22AM -0700, Steven Bethard wrote:
> (E-Mail Removed) wrote:
> >Since Python does not have declarations, I wonder if people think it is
> >good to name function arguments according to the type of data structure
> >expected, with names like "xlist" or "xdict".

>
> In general, I find that naming collections for their contents is much
> more useful than some abbreviated type prefix. However, while I don't
> name objects by their type, I do tend to name iterables with plurals
> (e.g. "words", "feature_indices", "events", etc.) and I typically suffix
> mapping types with "map" (e.g. "word_index_map", "event_relation_map",
> "prime_factor_map", etc.)
>

Ditto for me, plural implies list and singular implies instance,
for (contact) in contacts:
# do something with contact

But I tend to name dictionaries as "key_to_value" as in
"email_to_contact" or "ip_to_hostname."

for (email) in emails:
contact = email_to_contact[email]
# do something with contact

It may seem verbose but I can type much faster than I can think and
it makes reading even forgotten code a breeze.

-Jack
 
Reply With Quote
 
Jack Diederich
Guest
Posts: n/a
 
      02-28-2005
On Mon, Feb 28, 2005 at 04:02:37PM -0500, Benji York wrote:
> Jack Diederich wrote:
> >Ditto for me, plural implies list and singular implies instance,
> >for (contact) in contacts:
> > # do something with contact

>
> May I ask why you place the parenthesis in the for statement?


I like the tuple-ness feel of it and frequently unpack multiple
values in for loops. I also like the visual feel, it makes it
easy to see what is being unpacked and what is the source.

"for (one, two, three) in somelist:"
versus
"for one, two, three in sometlist:"

Even with a colorizing editor (emacs) I find the first version
easier to read. YMMV.

-Jack

 
Reply With Quote
 
Just
Guest
Posts: n/a
 
      02-28-2005
In article <(E-Mail Removed)>,
Jack Diederich <(E-Mail Removed)> wrote:

> On Mon, Feb 28, 2005 at 04:02:37PM -0500, Benji York wrote:
> > Jack Diederich wrote:
> > >Ditto for me, plural implies list and singular implies instance,
> > >for (contact) in contacts:
> > > # do something with contact

> >
> > May I ask why you place the parenthesis in the for statement?

>
> I like the tuple-ness feel of it and frequently unpack multiple
> values in for loops. I also like the visual feel, it makes it
> easy to see what is being unpacked and what is the source.
>
> "for (one, two, three) in somelist:"
> versus
> "for one, two, three in sometlist:"
>
> Even with a colorizing editor (emacs) I find the first version
> easier to read. YMMV.


But you're using it for _single_ values. That's like writing

(val) = someFunction(...)

Just
 
Reply With Quote
 
Benji York
Guest
Posts: n/a
 
      02-28-2005
Jack Diederich wrote:
> Ditto for me, plural implies list and singular implies instance,
> for (contact) in contacts:
> # do something with contact


May I ask why you place the parenthesis in the for statement?
--
Benji
 
Reply With Quote
 
Jack Diederich
Guest
Posts: n/a
 
      02-28-2005
On Mon, Feb 28, 2005 at 09:41:37PM +0100, Just wrote:
> In article <(E-Mail Removed)>,
> Jack Diederich <(E-Mail Removed)> wrote:
>
> > On Mon, Feb 28, 2005 at 04:02:37PM -0500, Benji York wrote:
> > > Jack Diederich wrote:
> > > >Ditto for me, plural implies list and singular implies instance,
> > > >for (contact) in contacts:
> > > > # do something with contact
> > >
> > > May I ask why you place the parenthesis in the for statement?

> >
> > I like the tuple-ness feel of it and frequently unpack multiple
> > values in for loops. I also like the visual feel, it makes it
> > easy to see what is being unpacked and what is the source.
> >
> > "for (one, two, three) in somelist:"
> > versus
> > "for one, two, three in sometlist:"
> >
> > Even with a colorizing editor (emacs) I find the first version
> > easier to read. YMMV.

>
> But you're using it for _single_ values. That's like writing
>
> (val) = someFunction(...)


Your Milage May^H^H^HDoes Vary *wink*
A quick grep of my source shows zero unparenthesized for loops,
266 with multiple unpacks and 492 iterating over single values.
Actually a bit closer to even, 96 are 'for (i) in range(len(l)):'
that were written before enumerate() came about.

I just always use parenthesis in for loops and when creating/upacking
tuples. I find it easier to read, except in the '(var) = func()' case.
Other people never use them. *shrug* I find this impossible to get
worked up about. What other people do in the privacy of their own
codebase doesn't bother me one bit.

My $0.01 bits,

-Jack
 
Reply With Quote
 
Paul Boddie
Guest
Posts: n/a
 
      03-01-2005
(E-Mail Removed) wrote in message news:<(E-Mail Removed) roups.com>...
> Since Python does not have declarations, I wonder if people think it is
> good to name function arguments according to the type of data structure
> expected, with names like "xlist" or "xdict".


Your suggestion coincides partly with a mechanism I developed recently
for my libxml2dom package. The normal libxml2dom package puts a
DOM-style wrapper around the existing Python wrapper objects -
something that Python's dynamic features accomplish easily - but this
incurs a major performance cost. Given that a low-level API
(libxml2mod) exists and provides a means to exchange fairly simple
and/or opaque objects with the library, I wondered if I couldn't just
write a code transformer which takes DOM-like code and emits code to
use the low-level API. For example:

element.childNodes -> libxml2mod.children(element)

The challenge, as you've noted with your mention of declarations, is
to find out whether a particular name refers to an object of a
suitable type for the kind of transformations I have in mind.
(Alternatively, one could just guess that "childNodes" is a DOM-style
attribute and do the transformation, possibly leading to mistaken
transformations taking place.) Whilst type inference might offer a
solution, it is itself a much bigger project than this, so my quick
solution was to permit the annotation of names using prefixes to
indicate which names refer to DOM-style objects. For example:

# Special magic defined earlier says that x2_ is the chosen prefix.
x2_element.childNodes -> libxml2mod.children(x2_element)

The result of this is libxml2macro [1], an experimental interface to
libxml2 which manages to retain much of the space/time performance of
that library, albeit without addressing the divisive issues of
transparent memory management. It also offers an insight into the
"optional static typing" parallel universe for Python...

Paul

[1] http://www.boddie.org.uk/python/libxml2dom.html
 
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
updating dictionaries from/to dictionaries Brandon Python 12 08-15-2008 12:35 AM
Pickling dictionaries containing dictionaries: failing,recursion-style! lysdexia Python 6 12-02-2007 12:03 AM
while executing my client program i get the exception javax.naming.LinkException: [Root exception is javax.naming.LinkException: [Root exception is javax.naming.NameNotFoundException: remaining if plz anybody know how to solve this problem then mahesh Java 0 03-08-2007 12:26 PM
WS C3550 naming convention question frishack@gmail.com Cisco 2 02-23-2005 07:29 PM
Which c# naming convention? Rick ASP .Net 1 01-19-2004 11:42 AM



Advertisments