noob question

Discussion in 'Python' started by Matt Hollingsworth, Jun 26, 2005.

  1. 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
    Matt Hollingsworth, Jun 26, 2005
    #1
    1. Advertising

  2. Matt Hollingsworth <> 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 <>
    Student of computational linguistics, Uppsala University, Sweden
    =?utf-8?q?Bj=C3=B6rn_Lindstr=C3=B6m?=, Jun 26, 2005
    #2
    1. Advertising

  3. Matt Hollingsworth

    Devan L Guest

    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.
    Devan L, Jun 26, 2005
    #3
  4. Matt Hollingsworth

    Guest

    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.
    , Jun 26, 2005
    #4
  5. 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.
    Steven D'Aprano, Jun 26, 2005
    #5
  6. Matt Hollingsworth

    Alan Gauld Guest

    "Matt Hollingsworth" <> wrote in message
    news:...
    > 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
    Alan Gauld, Jun 27, 2005
    #6
  7. 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-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>
    Fredrik Lundh, Jun 27, 2005
    #7
  8. 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.
    Steven D'Aprano, Jun 27, 2005
    #8
  9. 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
    Reinhold Birkenfeld, Jun 27, 2005
    #9
  10. 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 ''.split('@')])"
    bruno modulix, Jun 27, 2005
    #10
  11. Matt Hollingsworth

    Marco Guest

    Steven D'Aprano <> wrote in message news:<>...

    > 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.
    Marco, Jun 28, 2005
    #11
    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. Yttrium
    Replies:
    7
    Views:
    2,510
    Yttrium
    Oct 15, 2003
  2. NewsHound

    something of a noob question

    NewsHound, Jun 30, 2004, in forum: Perl
    Replies:
    1
    Views:
    414
    Jim Gibson
    Jun 30, 2004
  3. Chris Dunaway

    Really noob question about asp:Image control

    Chris Dunaway, May 27, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    1,030
    Chris Dunaway
    May 27, 2004
  4. haylow
    Replies:
    0
    Views:
    577
    haylow
    Jun 15, 2004
  5. Phil Winstanley [Microsoft MVP ASP.NET]

    Re: Noob question: "Could not find a part of the path"

    Phil Winstanley [Microsoft MVP ASP.NET], Jun 15, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    364
    Phil Winstanley [Microsoft MVP ASP.NET]
    Jun 15, 2004
Loading...

Share This Page