Re: [Tutor] Beginner Q: What does the double underscore mean __ ?

2012-09-09 Thread Dave Angel
On 09/09/2012 08:16 AM, Aaron Pilgrim wrote:
> Thank you for the response!
> 

You're welcome. A couple points of order:  please don't top-post.  There
is a standard here, which makes much more sense than the
Microsoft-imposed  "put everything backwards" approach.  Put your new
text AFTER whatever you're quoting.  Or don't bother quoting.

Second:  it's impolite to post a second copy of a question 20 minutes
after a first, especially on two closely related forums like
comp.lang.python and tutor.  Wait till you're sure you won't get a
response (on these forums, that might be 24 hours).  By posting twice,
you get two separate threads, and lots of duplicate information.

> It makes much more sense to me. I read the python documentation and
> was a little confused. I was gathering it was for operator
> overloading-that there was already a main and already a name function
> and these were special cases.
> 

Now you're asking specifically about the __name__ attribute.  That
exists in all modules, and is simply the file name that was imported
(without path and such).  And it's filled in for the script as well, but
given a unique value, regardless of the script's filename.

The PURPOSE, however, is to let programs tell whether a particular
source file was loaded as a script, or imported as a module.  It's a
common paradigm for modules to do self-testing when the source is loaded
as a script.  But you don't want that self-test code to be live when
some other program uses the module.  So you wrap it in the if statement:

if __name__ == "__main__":
if myfunc("abc") != 3:
error("myfunc is busted"
etc.



-- 

DaveA
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Beginner Q: What does the double underscore mean __ ?

2012-09-09 Thread Mark Lawrence

On 09/09/2012 12:27, Aaron Pilgrim wrote:

Hi,
I was trying to teach myself some python from a book and I have seen
double underscores used.
What does the __ mean and how do I know when to use it?

for example:

__name__==’__main__’

(source)
Pilgrim, Mark (2009-10-23). Dive Into Python 3 (Books for
Professionals by Professionals) (Kindle Locations 480-481). Apress.
Kindle Edition.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor



Start here http://docs.python.org/reference/datamodel.html#specialnames
Any questions please feel to ask as we don't bite :)

--
Cheers.

Mark Lawrence.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Beginner Q: What does the double underscore mean __ ?

2012-09-09 Thread Dave Angel
On 09/09/2012 07:27 AM, Aaron Pilgrim wrote:
> Hi,
> I was trying to teach myself some python from a book and I have seen
> double underscores used.
> What does the __ mean and how do I know when to use it?
>
> for example:
>
> __name__==’__main__’
>
> (source)
> Pilgrim, Mark (2009-10-23). Dive Into Python 3 (Books for
> Professionals by Professionals) (Kindle Locations 480-481). Apress.
> Kindle Edition.
> ___
>

Two places I know of where both leading and trailing underscores are
used in Python.  One is for special attributes like __name__ and __doc__
and the other is for special methods like __len__ and __str__

__name__ and __file__ are attributes of a module, which let you find out
things about what source file was imported to get that modjule. The name
"__main__" is used to flag the SCRIPT that you started;  nobody imports
that file, that's how you started this program running.

__doc__ is filled in by the compiler when it detects what's called a
docstring:  a literal string immediately following a def statement, a
class statement. The module docstring would have to be the first
executable line in the module, following the shebang and the encoding line.

The special methods are defined in a class to give it special behavior. 
For example, if a class acts like a collection, it'd be convenient to
define what the len() function does, so that it shows how many elements
are in the instance.  So, under the covers, the built-in len() function
calls the objects __len__() method.  A second example you use many times
without noticing is that when you call str() on an object, it looks for
a __str__() method.  If found, that's how the object is displayed.  If
not, you get the generic <__main__.X instance at 0x267da70> format
(differs by Python version).  Incidentally when you print an arbitrary
object, print calls str(), which may call __str__().

There are very few times you actually use such attributes or call such
methods;  __name__ is the only common one.  For example, you should use
str(), not __str__().  And you should use  a[4], not __getitem__

See http://docs.python.org/reference/datamodel.html, and search on that
page for "special methods"




-- 

DaveA

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Beginner Q: What does the double underscore mean __ ?

2012-09-09 Thread Aaron Pilgrim
Hi,
I was trying to teach myself some python from a book and I have seen
double underscores used.
What does the __ mean and how do I know when to use it?

for example:

__name__==’__main__’

(source)
Pilgrim, Mark (2009-10-23). Dive Into Python 3 (Books for
Professionals by Professionals) (Kindle Locations 480-481). Apress.
Kindle Edition.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor