[Tutor] need an explanation

Emile van Sebille emile at fenx.com
Thu Oct 11 22:27:19 CEST 2012

Matthew Ngaha wrote:
> i need help on 2 topics.
> 1) can someone please tell me what sys is doing, and why its using weird 
> indexing?
> if __name__ == "__main__":
>     A_Class(*sys.argv[1:4]).A_Class_Method()

sys is doing nothing -- argv in sys holds the command line arguments 
passed into python.  The first sys.argv[0] is the python script being 
executed, and the rest sys.argv[1:] are arguments passed in to that 
script.  specifying sys.argv[1:4] means you're picking just the three 
items.  *sys.argv[1:4] expands those from their list form and are passed 
into A_Class's __init__ constructor (assuming old style classes).  This 
instantiates an instance of that class then invokes the A_Class_Method 
of that instance.

> is sys able to call methods? if so why does it need indexing if it uses * .
> ------------------------------------------------------------------------------------------------------
> 2) also i need help with zipfiles. these 2 functions are related in the 
> same class.
> def __init__(self):
>     self.zipping_directory = "unzipped-{}".format(filename)
> def _full_filename(self, filename):
>         return os.path.join(self.zipping_directory, filename)
> def zip_files(self):
>         file = zipfile.ZipFile(self.filename, 'w')
>         for filename in os.listdir(self.zipping_directory):
>             file.write(self._full_filename(filename), filename)
> the main thing i need help with is the last line. the zip file is 
> writing to a file but why does it use the same argument twice? 

the first is passed into the instance's _full_filename method and the 
result of that becomes the first argument passed into file.write, and 
the second is passed in the file.write as the second argument.



> the for 
> loop above that line returns the file from the zipping directory, which 
> is the 2nd argument on file.write? But the 1st argument is using that 
> same file because that is the file returned from the def 
> _full_filename(self, filename): method. so please can someone tell me 
> why it uses the same file argument twice in its write method?
> thanks for your time
> ------------------------------------------------------------------------
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

More information about the Tutor mailing list