Starting Python... some questions

Bruno Desthuilliers bruno.42.desthuilliers at wtf.websiteburo.oops.com
Tue Mar 13 09:38:20 CET 2007


jezonthenet at yahoo.com a écrit :
> I started using Python a couple of days ago - here are a few
> questions:
> 
> * Doesn't the __main__() method automatically execute when I run my
> python program?

Which "__main__" method ???

Anyway, the answer is no. Every code at the top-level (which includes 
import, class and def statements...) is executed when the module is 
first loaded - whether as a proper module (ie: imported from somewhere 
else) or as a main program (ie-> python mymodule.py). In the first case, 
the special variable __name__ is set to the module's name, in the second 
to '__main__'. So you can rely on this to have code executed when the 
module is used as a program. The usual idiom is

# mymodule.py
(imports here)
(classes and defs here)

def main(argv):
   (code of main function here)
   return 0

if __name__ == '__main__':
   import sys
   sys.exit(main(sys.argv))


> * Only when I do an import of my test.py file within python and then
> run test.__main__() 

BTW, you should *not* use names with 2 leadings and 2 trailing 
underscores. These names are reserved for Python implementation stuff.

> I can see where my bugs are. Is this correct?

Nope. You can also spot bugs by reading the code or running it thru the 
debugger !-)

More seriously : using the above idiom (or any variant of - the 
important part being the conditional on __name__ == '__main__'), you can 
just
$ python mymodule.py

to run your program.

> (right now this is my only way of running my python program and see
> where I have problems)

> * Once I've done an import and then I wish to make a change to the
> file I've imported I have to quit Python, restart and import that
> module again in order for the module to be refreshed. Is there no "re-
> import" ?

reload(module_object)

But it's of very limited use. The best thing to do is usually to have a 
simple test file that setup the desired state (imports etc) that you 
execute after each change, passing the -i option to the python 
interpreter (this will leave the interpreter in interactive mode after 
execution of the test file, so you can inspect your objects, test things 
etc).

> * Finally, could someone tell me why I'm having problems with the
> small module below?
>   - Python pretends I provide chassis_id() with three parameters, even
> though I clearly only provide it with two - why?

Without even reading the code, I can tell you it's an instance or 
classmethod with either a wrong declaration or wrongly called.

> 
> #!/usr/bin/python
> import scapy
> import struct
> 
> class lldp_class:

Do yourself a favor: use new-style classes. Also, it would be better to 
stick to usual naming conventions (Python relies heavily on conventions):
http://www.python.org/dev/peps/pep-0008/

   class Lldp(object):

> 	def __init__(self):
> 		self.chassis_id_tlv = None
> 
> 	def chassis_id(subtype, chassis_info):

Bingo. You need to have self as the first argument. The instance is 
passed as the first argument of a method.

         def chassis_id(self, subtype, chassis_info):

> 		if subtype == 4:
> 			chassis_data = struct.pack("!B",chassis_info)
> 		subtype_data = struct.pack("!B",subtype)
> 		self.chassis_id_tlv = subtype_data + chassis_data
> 
> def __main__():


   def main():

> 	p = lldp_class()
> 	p.chassis_id(4, "01:80:C2:00:00:0E")

For the record: this is interpreted as:
         lldp_class.chassis_id(p, 4, "01:80:C2:00:00:0E")


> 	payload = p.chassis_id_tlv
> 	ether = scapy.Ether(dst="01:02:03:04:05:06")
> 	fullpayload = ether + payload
> 	sendp(fullpayload)
> 

if __name__ == '__main__':
     main()

As a side note, it looks like there are a couple point where your design 
may be improved. Like passing subtype and chassis_info to the __init__ 
of your class.

HTH



More information about the Python-list mailing list