noob question

M

Matt Hollingsworth

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
 
G

Guest

Matt Hollingsworth said:
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.
 
D

Devan L

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

qwweeeit

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

Steven D'Aprano

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?
 
A

Alan Gauld

Matt Hollingsworth said:
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?
 
F

Fredrik Lundh

Alan said:
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-us/dnvs600/html/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>
 
S

Steven D'Aprano

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
 
R

Reinhold Birkenfeld

Alan said:
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
 
M

Marco

Steven D'Aprano said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top