Please explain Python "__whatever__" construct.

Discussion in 'Python' started by bsagert@gmail.com, Jun 16, 2008.

  1. Guest

    After a couple of weeks studying Python, I already have a few useful
    scripts, including one that downloads 1500 Yahoo stock quotes in 6
    seconds. However, many things are puzzling to me. I keep on seeing
    things like "__main__" in scripts. A more obscure example would be
    "__add__" used in string concatenation. For example, I can use "Hello
    "+"world (or just "Hello" "world") to join those two words. But I can
    also use "Hello ".__add__("world"). When and why would I ever use
    "__main__" or the many other "__whatever__" constructs?
     
    , Jun 16, 2008
    #1
    1. Advertising

  2. On Jun 16, 2:56 pm, wrote:
    > After a couple of weeks studying Python, I already have a few useful
    > scripts, including one that downloads 1500 Yahoo stock quotes in 6
    > seconds. However, many things are puzzling to me. I keep on seeing
    > things like "__main__" in scripts.  A more obscure example would be
    > "__add__" used in string concatenation. For example, I can use "Hello
    > "+"world (or just "Hello" "world") to join those two words. But I can
    > also use "Hello ".__add__("world"). When and why would I ever use
    > "__main__" or the many other "__whatever__" constructs?


    http://docs.python.org/lib/genindex.html#letter-_
     
    Jason Scheirer, Jun 16, 2008
    #2
    1. Advertising

  3. Guest

    On Jun 16, 4:56 pm, wrote:
    > After a couple of weeks studying Python, I already have a few useful
    > scripts, including one that downloads 1500 Yahoo stock quotes in 6
    > seconds. However, many things are puzzling to me. I keep on seeing
    > things like "__main__" in scripts. A more obscure example would be
    > "__add__" used in string concatenation. For example, I can use "Hello
    > "+"world (or just "Hello" "world") to join those two words. But I can
    > also use "Hello ".__add__("world"). When and why would I ever use
    > "__main__" or the many other "__whatever__" constructs?


    Generally, names with two leading and trailing underscores signal
    something "internal". Though the string "__main__" is rather something
    else: the variable __name__ is set to the string "__main__" when a
    script is run as a script (i.e., is not imported). The convention is
    also common in built-in object methods, such as the one you mentioned:
    the built-in type str's __add__() method. Personally, I usually try to
    avoid using such methods directly, because, as I said, they're rather
    for internal use or for special functionality. For example, when the
    expression '"hello" + "world"' is evaluated, it's likely that Python
    is calling one of the string's __add__() method internally to perform
    the "addition." So I'd recommend that you don't use those methods
    unless you absolutely need direct access to their functionality.
     
    , Jun 17, 2008
    #3
  4. Matimus Guest

    When and why would I ever use
    > "__main__" or the many other "__whatever__" constructs?


    You don't generally use those names directly, they are 'magic'. The
    __add__ example is a good one. When you do `"hello " + "world"` behind
    the scenes python is actually calling "hello ".__add__("world").

    There are a couple of places though that you do use them. "__main__"
    is a good example. That is the name of the `main` module. The module
    attribute `__name__` is the name of that module. If the code is being
    executed as a script the value of `__name__` is set to "__main__".
    Hence, if you create a module and you want to execute some code only
    if that module is run as a script you can use this construct:

    if __name__ == "__main__":
    # do stuff

    Here is an example of a the `__name__` attribute when it isn't
    "__main__":

    >>> import sys
    >>> sys.__name__

    'sys'

    Also, these names are frequently used when creating a class where you
    want special behavior.

    >>> class myint(object):

    .... def __init__(self, a): # The constructor
    .... self.a = a
    ....
    .... def __add__(self, x):
    .... print "I'm adding"
    .... return self.a + x
    ....
    >>> x = myint(10)
    >>> x + 12

    I'm adding
    22

    As an added note, `"hello " "world"` is not concatenating two strings,
    The parser just sees it as one string. Otherwise, this would also
    work:

    >>> x = "hello "
    >>> x "world"

    File "<stdin>", line 1
    x "world"
    ^
    SyntaxError: invalid syntax

    Where:

    >>> x = "hello "
    >>> x + "world"

    'hello world'

    Matt
     
    Matimus, Jun 17, 2008
    #4
    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. KK
    Replies:
    2
    Views:
    597
    Big Brian
    Oct 14, 2003
  2. Replies:
    10
    Views:
    610
    Jeffrey Schwab
    Nov 1, 2006
  3. Mukesh
    Replies:
    4
    Views:
    626
    Paul N
    Mar 26, 2010
  4. Kaye Ng
    Replies:
    8
    Views:
    178
    Josh Cheek
    Jun 8, 2010
  5. Replies:
    1
    Views:
    111
    Thomas 'PointedEars' Lahn
    Dec 31, 2007
Loading...

Share This Page