Home > other >  How to create a global class object
How to create a global class object

Time:04-10

I have a API. Py, defines a class that has a login, query two functions, need to log in to the query, using requests. The Session () save the login status, pseudo code is as follows:
 
"" "API. Py "" "

The import requests

The class Api:
_session=requests. The Session ()

Def the login () :
_session. Post (... )

Def query () :
_session. Post (... )


Define a public module Shared. Py, inside the class instantiation, pseudo code is as follows:
 
"" "Shared. Py "" "

The import API
The instance=API. The API ()


Have other module1. Py, module2. Py two files, in module1. Login in py, in module2. Py in the query, pseudo code is as follows:
 
"" "module1. Py "" "

The import Shared
Shared. The instance. The login ()


 
"" "module2. Py "" "

The import Shared
Shared. The instance. The query ()


I'll perform module1. Py, after execution of module2, py, module2 executes found in not login state, that is to say in module1 and module2, Shared. The instance is not the same object, result in requests. The Session () is not the same Session,

My question is, how to create a global class object, make two different can access to the same object in the module?
  • Related