-1

I am trying to develop a robot for Skype and I am having difficulties with certain classes and how to use only one instance instead of creating a new one within another file.

I have many files in which the robot runs from and I want to only use one instance via a Global.py file:

from classes import class_Core
from classes import class_Skype
from classes import class_Message
from classes import class_Commands

global Core, Skype, Message, Commands

Core = class_Core.Core()
Skype = class_Skype.Skype()
Message = class_Message.Message()
Commands = class_Commands.Commands()

I also have a Skypebot.py file which imports the Global.py file:

import Global

However, when I run Skypebot.py and use a function from Core in the Skype class:

class Skype:
    def __init__(self):
        Core.Log("Initialising Skype!")

I get an trace back:

Traceback (most recent call last):
  File "C:\Users\Connor\Desktop\Skypebot 0.4\Skypebot.py", line 1, in <module>
    import Global
  File "C:\Users\Connor\Desktop\Skypebot 0.4\Global.py", line 9, in <module>
    Skype = class_Skype.Skype()
  File "C:\Users\Connor\Desktop\Skypebot 0.4\classes\class_Skype.py", line 8, in
 __init__
    Core.Log("Initialising Skype!")
NameError: global name 'Core' is not defined

Can anybody help me on this? Thanks!

Connor Simpson
  • 487
  • 1
  • 7
  • 27
  • possible duplicate of [How do I access inherited variables from another module when they are changed in parent?](http://stackoverflow.com/questions/19358582/how-do-i-access-inherited-variables-from-another-module-when-they-are-changed-in) – Joel Cornett Apr 03 '14 at 19:35
  • Declear those global variables inside the function where u are using it. – Arvind Apr 03 '14 at 19:37
  • I don't think so, @JoelCornett, but thanks! I'm not trying to inherit any functions, just use instances from the Global.py file in other classes without making new instances within the classes. – Connor Simpson Apr 03 '14 at 19:38
  • @Arvind I'm still getting that error, but thanks! – Connor Simpson Apr 03 '14 at 19:40
  • The `global` keywords only applies to the current file, not the entire package. You may need to change your program layout so that all the classes are instantiated in the main routine, and pass them as parameter to the other classes which need them. This way, they'll only hold a pointer to the instances. – michaelmeyer Apr 03 '14 at 19:42
  • Can I have an example of what you mean @doukremt? – Connor Simpson Apr 03 '14 at 20:07
  • **There is no such thing as a global variable in python**. Just remove this concept from your mind. There are only *module* attributes. If you want to use `Core` in your `Skype` then you *must* import it. Also I believe you should *not* put those classes into separate modules. You'll just create circular references if you continue like that. If some classes are interrelated they should stay together (even though Java doesn't agree, but who cares?). – Bakuriu Apr 03 '14 at 21:30

1 Answers1

0

You want this in skypebot.py, instead of import Global:

from Global import Core

You also don't need the global Core, Skype, Message, Commands line, since those variables are already being declared at a global scope within Global.py, as doukremt mentioned. The "global" keyword would be used if you then wanted to access/modify a global variable instance a function or method, e.g.

var = 3
def some_func():
    var = 2  # This creates a locally-scoped variable named var, and assigns it to 2.

def some_other_func():
    global var
    var = 2 # This sets the globally-scoped variable var to 2.
dano
  • 91,354
  • 19
  • 222
  • 219