questions.part #2

Jeff Shannon jeff at ccvcorp.com
Thu Aug 9 21:47:43 EDT 2001


whats_really_hot wrote:

> 1. When we type "dir()" in the interactive command line prompt get among all
> the others the module "__name__" which really tell us the name of the module
> that we are . But now we are in the interactive command line prompt, which
> is a module with the name "__main__". Now, my question is, why isn't
> __name__ = __main__? (Why isn't __name__ substituted by __main__?
>
> Sorry for repeating this question but I didn't really get a clear answer.
>

Well, you did, you just didn't understand it.  :)

__name__ is a variable, just like any variable that you use in Python.  Every
variable is a name, which is bound to a value.  In this case, the name of the
variable is __name__, and the value of the variable is set, by the interpreter,
to the name of the current module.  While running in interactive mode, or when
running a module directly as a script, the current module name is assumed to be
__main__.  What you seem to expect, is that some string in the dir() would be
equal to the module name, but this is not correct.  dir() lists all of the
variable names in the current namespace.  In this case, one of those variables
is *named* __name__, and that variable has a *value* of __main__.  So, for
example:

>>> def isimported():
...    if __name__ == '__main__':
...       print "I am a top-level script!"
...    else:
...       print "I am an imported module!"
...
>>> isimported()
I am a top-level script!
>>> print __name__
__main__
>>>

Is that more clear?

> 2. What is the difference between "!=" and "<>" (which really mean
> "different")?

No real difference, it's a matter of style. "<>" is Pascal-style, and is favored
by those coming from that language or its relatives;  "!=" is C-style, and seems
to be preferred by Guido.  But at this point in time, either one is usable.

> 3. Are the built-in tools (such as "len", "sort" etc) maintained in a folder
> into the Python directory, so as I can access and edit them?

The built-in tools are part of the compiled interpreter, and are implemented in
C.  If you want to look through the C source, you can, and if you wish to change
them and recompile the interpreter, you can, but it's probably not a good idea
unless you really know what you're doing, or really like poking things to see if
they break.  :)

> 4.   What is the difference between "%d" and "%i" (which really point
> integer values)?

I think that the main difference is historical, relating to C's sprintf()
function.  I'd just use %i.

> 5. What does it mean that Python is a "high-level language" and other
> languages, such as C, are low-level?

C is very close to the machine architecture.  You can twiddle individual bits,
you can access specific memory addresses, you can directly access I/O ports, and
all sorts of little details.  This gives you a great amount of power, but to use
it well, you have to know what do with it, and how to do so safely--it includes
the power to shoot yourself in the foot.  Python manages many of these details
for you.  It handles all aspects of memory management, so that, while you may
not be able to access an exact memory address, you also don't have to worry
about writing over the *wrong* part of memory.  It provides an insulating layer
over I/O ports, so that, while you may not be able to do things quite as
efficiently as in C, it's much simpler.  Most importantly, Python provides quite
a few advanced data and control-flow structures--lists, dictionaries, smart
for-loops, etc.  You can do all of these things in C, but it would take quite a
bit of code, and have the potential to be very buggy.  In Python it's all done
for you, already.  And because it operates at a higher level of abstraction, you
can typically write the same functionality in much less code.  400 to 500 lines
of  C can easily be reduced to less than 100 lines of Python.  Less code means
less chance for bugs, typos, etc.

> Sorry for the simplicity of some of the above questions, but are crucial for
> me for the full understanding of Python.
>
> Thanks again for ur attention and immediate responds.
>

Hope that helps...

Jeff Shannon
Technician/Programmer
Credit International





More information about the Python-list mailing list