473,396 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

naming convention for scalars, lists, dictionaries ...

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

Jul 18 '05 #1
8 6659

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
Jul 18 '05 #2
be*******@aol.com 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
Jul 18 '05 #3
On Mon, Feb 28, 2005 at 11:32:22AM -0700, Steven Bethard wrote:
be*******@aol.com 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
Jul 18 '05 #4
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
Jul 18 '05 #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

Jul 18 '05 #6
In article <ma***************************************@python. org>,
Jack Diederich <ja**@performancedrivers.com> 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
Jul 18 '05 #7
On Mon, Feb 28, 2005 at 09:41:37PM +0100, Just wrote:
In article <ma***************************************@python. org>,
Jack Diederich <ja**@performancedrivers.com> 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
Jul 18 '05 #8
be*******@aol.com wrote in message news:<11**********************@z14g2000cwz.googleg 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
Jul 18 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: cmiddlebrook | last post by:
Hi there, I keep finding myself getting inconsistent with naming conventions for things like member variables, class names etc and I just want to find something that suits me and stick to it. I...
5
by: aurora00 | last post by:
I use generators a lot. E.g. def gen_words(text) ... parse text ... yield each word in text for word in gen_words(text): print word
114
by: Jonathan Wood | last post by:
I was just wondering what naming convention most of you use for class variables. Underscore, "m_" prefix, camel case, capitalized, etc? Has one style emerged as the most popular? Thanks for...
23
by: Thorsten Kampe | last post by:
Okay, I hear you saying 'not another naming conventions thread'. I've read through Google and the 'naming conventions' threads were rather *spelling conventions* threads. I'm not interested...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.