Goodnight! There is a file Myfile1.py, in which there are functions with global variables

class setup(): def 00(self): global abc abc = "hello" 

and there is a file Myfile2.py, in which this variable should be used

 class dosmth(): def 01(self): print abc 

Question: how do I import and run the function in myfile2.py?

  • one
    This is not Python code. - jfs

1 answer 1

In order to start the function in Myfile2.py , import the Myfile1.py module and access it through a dot:

 import Myfile1 Myfile1.имя_Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΈ() 

Or import only variable names and functions that you need:

 from Myfile1 import your_func, your_var1, your_var2 

Then you can contact them without a module name.

To access functions from another class, create an instance of it and call the methods you need:

 import Myfile1 o = Myfile1.setup() o.имя_Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΈ() 
  • And how can I use global variables from another? The global variable abc is defined in the function (accesstoken when registering a new user). Unfortunately, for some reason these variables do not cling - Sergey
  • @ Sergey, Try using the global . For example: global abc - Klym
  • @ Sergey: in the example in the answer, setup is a global name from the module Myfile1. If you can use setup() , then absolutely you can also access other global variables from the Myfile1 module. Aside: do not use global variables unnecessarily (for example, why not make abc, a class variable? Or even an ordinary attribute of your object?). Use the conventions from pep-8 - jfs