Usually the python convention of self is to be used in python classes, you did a bit of a mess.
So either you are not using classes and treating self just as a global dict, like this:
import sys
myglobal = {} # Didn't want to name it self, for avoiding confusing you :)
def otherFunction():
print myglobal["tecE"]
def main(argv):
myglobal["tecE"] = 'test'
otherFunction()
if __name__ == "__main__":
main(sys.argv[1:])
Or writing a class, like this:
import sys
class MyClass():
def otherFunction(self):
print self.tecE
def main(self, argv):
self.tecE = 'test'
self.otherFunction() # Calling other class members (using the self object which actually acting like the "this" keyword in other languages like in Java and similars)
if __name__ == "__main__":
myObj = MyClass() # Instantiating an object out of your class
myObj.main(sys.argv[1:])
So how and where to define self?
You will use self:
- As the first argument of your class methods
def my_method(self, arg1, arg2):
- Within the class to refer to any other class members (just as demonstrated above)
self.do_job("something", 123)
- For creating class members:
self.new_field = 56 Usually in __init__() constructor method
Note: decalring a class variable without the self.new_var, will create a static class variable.
selfin main?selfis normally only used in combination with classes ... I assume you copy pasted this code (mainly the different functions) from somewhere else (where the class-keyword has been used) - without understanding it.thisis used. Wouldn't mind to make a class out of it, but is there some kind of template for the usage of global variables and parameters for a simple script to be run in the command line?thisorself, they are just variable names. No matter if within or outside of a class. You have to define them just like any other variable. It's just a convention to useselfwithin a class to reference the current class instance.this?thisis Java, not Python. The python class syntax is a bit different then the java syntax stackoverflow.com/questions/64141/classes-in-python