drwxr-xr-x 2 f012e8f4 w0134aab 4096 Mar 26 12:03 . drwxr-xr-x 6 f012e8f4 w0134aab 4096 Mar 18 10:36 .. -rw-r--r-- 1 f012e8f4 w0134aab 3837 Mar 20 08:45 author.php -rw-r--r-- 1 f012e8f4 w0134aab 745 Mar 18 10:39 backend-errors.php -rw-r--r-- 1 f012e8f4 w0134aab 4392 Mar 31 08:25 meta_boxes.php -rw-r--r-- 1 f012e8f4 w0134aab 6837 Apr 17 09:53 toc.php Python – Pass variables to imported modules – Talha Sariyürek

Python – Pass variables to imported modules

How to split your functions into modules and still keep the variables.

    Inhaltsangabe
  1. Passing variables
  2. Summary

I recently had the problem that I could not pass my FTP object into a module that I had written myself.

The problem was that every time I had to reconnect to the FTP server and lost a lot of time.

Passing variables

Let’s say we have two Python script files. One of these is just the main script and the second file is check.py

The check.py is located in a subfolder called lib and is imported from there into my main script.

If we do that, the main script variables will not automatically be present in check.py. However, you can simply pass these by executing the following …

...
...
from lib import check
check.ftp_obj = ftp_obj
myCoolFuntion('some_file.css')
...
...
def myCoolFuntion(file):
  ftp_datetime = ftp_obj.voidcmd("MDTM " + file)[4:].strip()
  ftp_timestamp = time.mktime(time.strptime(ftp_datetime, '%Y%m%d%H%M%S')) 
  print(int(ftp_timestamp))

Summary

With this method you can pass on as many variables as you like to another script. I used that with my ftp object and it worked as well. Have fun with it!

Topics

Experience Program python technology Tips

Beitrag teilen

WhatsApp it

Folgen Sie uns