Please explain Python "__whatever__" construct.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • bsagert@gmail.com

    Please explain Python "__whatever__" construct.

    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__("worl d"). When and why would I ever use
    "__main__" or the many other "__whatever __" constructs?
  • Jason Scheirer

    #2
    Re: Please explain Python "__whateve r__" construct.

    On Jun 16, 2:56 pm, bsag...@gmail.c om 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__("worl d"). When and why would I ever use
    "__main__" or the many other "__whatever __" constructs?

    Comment

    • s0suk3@gmail.com

      #3
      Re: Please explain Python "__whateve r__" construct.

      On Jun 16, 4:56 pm, bsag...@gmail.c om 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__("worl d"). 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.

      Comment

      • Matimus

        #4
        Re: Please explain Python "__whateve r__" construct.

        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__("worl d").

        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

        Comment

        Working...