[Tutor] under, under
Please explain the significance of __some term__. For example __name__ as in If __name__ == '__main__': main() When is the under, under used? Regards, Stafford ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] under, under
On 05/13/13 17:21, Stafford Baines wrote: When is the under, under used? It depends the context, for example __name__ represent the name of the current package. The __init__ it's object methode to initialize the object, and so on. -- \0/ Hobbestigrou site web: erakis.eu L'Europe est trop grande pour être unie. Mais elle est trop petite pour être divisée. Son double destin est là ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] under, under
I have seen (and enjoy) people calling double underscore as 'Dunder' On Mon, May 13, 2013 at 12:32 PM, Natal Ngétal hobbestig...@erakis.euwrote: On 05/13/13 17:21, Stafford Baines wrote: When is the under, under used? It depends the context, for example __name__ represent the name of the current package. The __init__ it's object methode to initialize the object, and so on. -- \0/ Hobbestigrou site web: erakis.eu L'Europe est trop grande pour être unie. Mais elle est trop petite pour être divisée. Son double destin est là ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor -- Joel Goldstick http://joelgoldstick.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] under, under
On 05/13/2013 12:21 PM, Stafford Baines wrote: Please explain the significance of __some term__. For example __name__ as in If __name__ == '__main__': main() When is the under, under used? (Please don't start a second thread with identical content 20 minutes after the first) Underscores aren't anything special to the Python language itself, whether leading or trailing. Thus there is no implicit connection between __name__ and name, for example. However there is a convention for single and double underscores, and when the latter are at both start and end of a symbol, they have the cute nickname of dunder. Dunder names are ones defined by the language as having special purpose. We should never make up our own such names, as we might conflict with a dunder name that gets added in a later version of Python. There are a few of them that are just data. One example is the __name__ builtin, and it is defined automatically by the import mechanism. And since the script itself is sort-of imported, it gets a special name of a literal __main__ This lets you write code that behaves differently when run as a script then when it's imported explicitly from another module or script. Most are methods, and these method names are called special methods. The __init__() method for initializing is the most important, since it's implicitly called when a class instance is being initialized. Likewise __new__(). Another (__str__()) is called implicitly when you try to interpret an object as a string (such as when you print it). The debugger uses the __repr__() special method. When you use the addition syntax a + b you'll be using the __add__() and/or the __radd__() methods. All these are pre-defined for the built-in types. And you can see such a list of them for a given type by doing something like: a = list() print dir(a) In the debugger, you might get: dir([]) ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort'] A key point is you can defined these in your own classes. So you can define for example what it means for instances to be equal, or how you add them, or subscript them. Normally, you do not directly call most of these special methods, they'll be called implicitly by various other means. But you do write them in your code. See: http://docs.python.org/2/reference/datamodel.html#special-method-names for a start. -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] under, under
On Mon, May 13, 2013 at 10:21 AM, Stafford Baines staffordbai...@yahoo.com wrote: Please explain the significance of __some term__. For example __name__ as in If __name__ == ‘__main__’: main() When is the under, under used? Section 2.3.2 http://docs.python.org/3/reference/lexical_analysis.html#reserved-classes-of-identifiers Explain as follows: __*__System-defined names. These names are defined by the interpreter and its implementation (including the standard library). Current system names are discussed in the Special method names section and elsewhere. More will likely be defined in future versions of Python. Any use of __*__ names, in any context, that does not follow explicitly documented use, is subject to breakage without warning. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] under, under
On Mon, May 13, 2013 at 1:18 PM, Dave Angel da...@davea.name wrote: Underscores aren't anything special to the Python language itself, whether leading or trailing. I'm pretty sure you were just talking about dunder, dunder. Underscores in general do have special uses in the language. They're used to enable name mangling and to implicitly control star imports. In a class definition, a leading dunder without a trailing dunder enables name mangling with the class name: class Bar: def foo(self): self.__attr = 'spam' obj = Bar() obj.foo() obj._Bar__attr 'spam' A subclass with a different name will use a different mangling, so this provides a semi-private name. The purpose is to protect a private implementation detail in the base class from being modified by a subclass, either accidentally or intentionally. I won't debate the merits of this. Generally, however, one signals that an attribute is 'private' by using a single leading underscore. This is just a hint to other programmers. Name mangling is a compile-time operation. The compiler replaces all identifiers that have a leading dunder (and no trailing dunder) with the corresponding mangled name: Bar.foo.__code__.co_names ('_Bar__attr',) Another use of underscore is in a star import. To show this, create a module: import sys, imp sys.modules['mod'] = mod = imp.new_module(name='mod') Add two global variables to the module, one with a leading underscore: mod._foo = 'foo' mod.bar = 'bar' Do a star import. Observe that the name with the leading underscore was skipped: from mod import * '_foo' in locals() False 'bar' in locals() True Typically it's better to specify the names used in a star import by defining __all__: mod.__all__ = ['_foo', 'bar'] from mod import * '_foo' in locals() True ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] under, under
On 14/05/13 02:21, Stafford Baines wrote: Please explain the significance of __some term__. For example __name__ as in If __name__ == '__main__': main() When is the under, under used? Underscores are legal characters in names. So you can write: some_term = whatever() and it is a legal name. *Leading* underscores have a special meaning. A single leading underscore is considered to be private: _name = 42 means that _name should be considered private, hands off. Or at least, if you break it, you bought it. No guarantees are valid if you change a private value and things break. Names with Double leading and trailing UNDERscores (dunder) are reserved for Python's internal use. They get used for special methods, and a few other things. For example, to override the + operator, you write a class that defines __add__ and __radd__ methods. To override the == operator, you write a class that defines a __eq__ method. There are many examples of such, you can read the docs for a current list: http://docs.python.org/2/reference/datamodel.html#special-method-names __name__ is a special variable automatically created by Python. Modules and packages are automatically given a variable called __name__ which contains their name. When you are executing a module as a script, that variable gets set to the special value __main__ instead of the module's actual name. So you can detect whether your code is being run as a script with a tiny bit of boilerplate code: if __name__ == '__main__': ... -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor