By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,941 Members | 1,753 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,941 IT Pros & Developers. It's quick & easy.

noob question

P: n/a
Hello,

Very new to python, so a noob question. When I've written stuff in
JavaScript or MEL in the past, I've always adopted the variable naming
convention of using a $ as the first character (no, I don't use perl,
never have). Not possible in python. What are some good options that
people commonly use so that they can easily and quickly identify
variable with just a glance? When I was writing stuff in SoftImage
XSI/JScript, many of the examples in the SDK used just a leading lower
case o, but I'd like somethign that's more visible. I'm not a super
advanced user and am just starting out in python (pretty much just
working my way through Learning Python, at around page 160 or so).
Seems like an _extremely_ elegent language that is very easy to read, so
I suppose it's not really as much of an issue as it is with other
languages. Still, I would like to see what other people do and what are
some good ideas for this kind of thing.

Thank you for any and all ideas.

cheers,

-Matt

Jul 19 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Matt Hollingsworth <ma**@maudit.net> writes:
What are some good options that people commonly use so that they can
easily and quickly identify variable with just a glance?


Use an editor that has a Python syntax highlighting mode, for example
Emacs with python-mode, Vim, or the IDLE IDE that comes with Python.

--
Björn Lindström <bk**@stp.ling.uu.se>
Student of computational linguistics, Uppsala University, Sweden
Jul 19 '05 #2

P: n/a
To recognize variables that you have assigned, just look for
assignment. If your code is readible, and you know it well, you
shouldn't need the $ sign in front of everything.

Jul 19 '05 #3

P: n/a
Hi Matt,
I also am almost a newbie (in Python) and my approach to variable
naming
follows more or less the Hungarian Type Notation Defined.
To better explain, I use one char or two (in small case) as a prefix of
the name of
the variable (starting in capital letters).
The prefix identifies the "type" of variable. My choice of such
prefixes is (a short example):
s string (sStringVariable)
l list (lFileLines)
n numeric (nNumericVariable)
and so on...
Bye.

Jul 19 '05 #4

P: n/a
On Sat, 25 Jun 2005 22:06:08 -0700, Matt Hollingsworth wrote:
Hello,

Very new to python, so a noob question. When I've written stuff in
JavaScript or MEL in the past, I've always adopted the variable naming
convention of using a $ as the first character (no, I don't use perl,
never have). Not possible in python. What are some good options that
people commonly use so that they can easily and quickly identify
variable with just a glance?


Learn the language rather than relying on magic symbols.

I don't understand why you would need to have a special naming convention
to identify variables. Magic symbols just get in the way, especially in a
language like Python where everything is an object. (Except keywords and
operators, I suppose.)

Eg, when you do something like this:

myInstance = MyClass(something)
print myInstance.method(somethingelse)

everything except "print" is an object. Or rather, a name that references
an object.

Python doesn't really have variables as such. It has names and objects.
Most of the time, you can think of names as being like variables.

Can anyone think of some good, easy to understand examples of where
Python's name/object model differs from the variable/value model?
--
Steven.

Jul 19 '05 #5

P: n/a
"Matt Hollingsworth" <ma**@maudit.net> wrote in message
news:ma**************************************@pyth on.org...
Very new to python, so a noob question. When I've written stuff in JavaScript or MEL in the past, I've always adopted the variable naming convention of using a $ as the first character (no, I don't use

perl,

Can I ask why you did that?
Did someone tell you to or did you hit on the idea yourself?
Do you really find it useful? I mean do you often in your
Python coding find a problem with identifying your variables?

I'm curious because one of the things I like most about Python
is the fact I don't need to mess up my programs with spurious
characters like $ etc. And I never have any problem identifying
variables etc in my code. (The same is true of Object Pascal
in Delphi, my other favourite language).

The only place I've ever found Hungarian notation useful was
in C which is a weird mix of static typing and no-typing,
and there the prefix code gives a clue as to what kind of
value you might expect to find. But when I moved to C++ I
dropped the prefixes because they added no value.

In Python Hungarian notation is meaningless since variables
aren't typed anyway.

So I am curious as to why anyone would feel the need to introduce
these kinds of notational features into Python. What is the
problem that you are trying to solve?
--
Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld


Jul 19 '05 #6

P: n/a
Alan Gauld wrote:
In Python Hungarian notation is meaningless since variables
aren't typed anyway.


in real-life Python code, variables tend to be 'typed' in the hungarian
sense:

http://msdn.microsoft.com/library/en...hunganotat.asp

"/.../ the concept of 'type' in this context is determined by
the set of operations that can be applied to a quantity. The
test for type equivalence is simple: could the same set of
operations be meaningfully applied to the quantities in
questions? If so, the types are thought to be the same.
If there are operations that apply to a quantity in exclusion
of others, the type of the quantity is different."

</F>

Jul 19 '05 #7

P: n/a
On Mon, 27 Jun 2005 07:14:07 +0000, Alan Gauld wrote:
The only place I've ever found Hungarian notation useful was
in C which is a weird mix of static typing and no-typing,
and there the prefix code gives a clue as to what kind of
value you might expect to find. But when I moved to C++ I
dropped the prefixes because they added no value.

In Python Hungarian notation is meaningless since variables
aren't typed anyway.


Not quite true. Objects are typed in Python:

py> type('')
<type 'str'>
py> type(1)
<type 'int'>

but names ("variables", almost) can be dynamically changed from one type
to another:

py> x = 1 # x starts as an int
py> x = '~' # now it is a string
py> x = [1] # and now a list

Even though names can refer to an object of any type, the actual
objects themselves are always strongly typed: "hello" is always a
string, {} is always a dict, and so on.

The best use of Hungarian notation is the original use, as introduced in
the application development team at Microsoft (before the operating system
team got their hands on it, and broke it).

Rather than prefixing variable names with the type which the compiler
already knows (eg "sName" for a string, "iAge" for an integer), the
original use of Hungarian notation was to use a prefix which explains what
the data is for: the "type" of object, but not types which compilers
understand.

Here are some good uses for Hungarian notation:

ixCurrent = 0 # ix~ means index into a list or array
cFound = 0 # c~ means a count
dxObject = object.width() # d means difference

No compiler in the world can tell that adding the width of an object in
pixels to the count of how many objects were found gives a meaningless
result. But a human programmer should immediately see the bug in:

ixHam = dxEgg + cTomato

even if they don't remember what Ham, Egg and Tomato represent.

Remember that space probe a few years back that crashed into Mars because
some NASA engineer forgot to convert metric to imperial or vice versa?
That sort of mistake is easy to make when you have something like this:

initialSpeed = 3572.8 # feet per second
# ~~~
# five pages of code
# ~~~
deceleration = 3.5 # metres per second squared
# ~~~
# five more pages of code
# ~~~
timeNeeded = initialSpeed/deceleration

Now imagine seeing this line instead:

timeNeeded = iInitialSpeed/mDeceleration

With just a glance you can see that there needs to be a conversion from
imperial (i~) to metric (m~) or vice versa, or else your $20 billion
dollar space probe turns the engines off too early and becomes a $20
billion hole in the ground.

The history and justification for Hungarian notation is explained here:

http://www.joelonsoftware.com/articles/Wrong.html
--
Steven.

Jul 19 '05 #8

P: n/a
Alan Gauld wrote:
The only place I've ever found Hungarian notation useful was
in C which is a weird mix of static typing and no-typing,
and there the prefix code gives a clue as to what kind of
value you might expect to find. But when I moved to C++ I
dropped the prefixes because they added no value. In Python Hungarian notation is meaningless since variables
aren't typed anyway.


Unfortunately, the original concept of Hungarian is often misunderstood,
as shown by the article pointed out by Konstantin.

Reinhold
Jul 19 '05 #9

P: n/a
qw******@yahoo.it wrote:
Hi Matt,
I also am almost a newbie (in Python) and my approach to variable
naming
follows more or less the Hungarian Type Notation Defined.


which is seen as a bad practice anywhere outside Win32...

http://mindprod.com/jgloss/unmainnaming.html
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Jul 19 '05 #10

P: n/a
Steven D'Aprano <st***@REMOVETHIScyber.com.au> wrote in message news:<pa****************************@REMOVETHIScyb er.com.au>...
Can anyone think of some good, easy to understand examples of where
Python's name/object model differs from the variable/value model?


a = b = [ 1 ]

a and b are _not_ two variables, each with [ 1 ] as value, but just two
names for the same object (a list). In other variable-based languages,
the above is equivalent to:

a = [ 1 ]
b = [ 1 ]

in Python it is not.

..TM.
Jul 19 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.