naming convention for scalars, lists, dictionaries ...

Discussion in 'Python' started by beliavsky@aol.com, Feb 28, 2005.

  1. Guest

    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".
    , Feb 28, 2005
    #1
    1. Advertising

  2. 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
    Skip Montanaro, Feb 28, 2005
    #2
    1. Advertising

  3. 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
    Steven Bethard, Feb 28, 2005
    #3
  4. On Mon, Feb 28, 2005 at 11:32:22AM -0700, Steven Bethard wrote:
    > 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
    # 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
    Jack Diederich, Feb 28, 2005
    #4
  5. 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
    Jack Diederich, Feb 28, 2005
    #5
  6. Just Guest

    In article <>,
    Jack Diederich <> 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
    Just, Feb 28, 2005
    #6
  7. Benji York Guest

    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
    Benji York, Feb 28, 2005
    #7
  8. On Mon, Feb 28, 2005 at 09:41:37PM +0100, Just wrote:
    > In article <>,
    > Jack Diederich <> 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
    Jack Diederich, Feb 28, 2005
    #8
  9. Paul Boddie Guest

    wrote in message news:<>...
    > 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
    Paul Boddie, Mar 1, 2005
    #9
    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. Rick

    Which c# naming convention?

    Rick, Jan 19, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    3,761
    Paul Glavich
    Jan 19, 2004
  2. dm1608
    Replies:
    6
    Views:
    8,489
  3. Buck Turgidson

    JSP Method Naming Convention

    Buck Turgidson, Mar 2, 2004, in forum: Java
    Replies:
    1
    Views:
    1,735
    P.Hill
    Mar 2, 2004
  4. harry
    Replies:
    2
    Views:
    1,165
    harry
    Dec 8, 2004
  5. Roedy Green

    Naming Convention(s)

    Roedy Green, Sep 3, 2005, in forum: Java
    Replies:
    24
    Views:
    953
    Dale King
    Sep 12, 2005
Loading...

Share This Page