473,803 Members | 2,038 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 6679

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.c om 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_indice s", "events", etc.) and I typically suffix
mapping types with "map" (e.g. "word_index_map ", "event_relation _map",
"prime_factor_m ap", etc.)

STeVe
Jul 18 '05 #3
On Mon, Feb 28, 2005 at 11:32:22AM -0700, Steven Bethard wrote:
be*******@aol.c om 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_indice s", "events", etc.) and I typically suffix
mapping types with "map" (e.g. "word_index_map ", "event_relation _map",
"prime_factor_m ap", 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_val ue" as in
"email_to_conta ct" or "ip_to_hostname ."

for (email) in emails:
contact = email_to_contac t[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************ *************** ************@py thon.org>,
Jack Diederich <ja**@performan cedrivers.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************ *************** ************@py thon.org>,
Jack Diederich <ja**@performan cedrivers.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.c om wrote in message news:<11******* *************** @z14g2000cwz.go oglegroups.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.childNo des -> libxml2mod.chil dren(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.chil dNodes -> libxml2mod.chil dren(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
2485
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 am wondering if there are any naming conventions around that are deemed suitable by the general C++ community. I have googled around and I can't find much - mostly long lists of hungarian-like prefixes which is not really what I'm after.
5
7813
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
7889
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 any comments. --
23
2445
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 in camelCase versus camel_case or anything mentioned in 'PEP 8 -- Style Guide for Python Code'. What I'm looking for is hints or ideas how to name your variables and especially how to name functions, methods and classes.
0
9699
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9562
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10542
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10289
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10068
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7600
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6840
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4274
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2968
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.