-5

I'd like to use self (for global variables) and the parameters from the command-line in my Python Script but can't really get them to work.

def otherFunction(self)
    print self.tecE

def main(argv,self):
    self.tecE = 'test'
    otherFunction()

if __name__ == "__main__":
   main(sys.argv[1:],self)

This gives me an error:

    main(sys.argv[1:],self)
NameError: name 'self' is not defined

So how and where to define self?

9
  • What is self in main? Commented Nov 14, 2018 at 9:18
  • 3
    self is 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. Commented Nov 14, 2018 at 9:19
  • I'd like to use it in a way as this is 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? Commented Nov 14, 2018 at 9:20
  • There is no special handling of this or self, 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 use self within a class to reference the current class instance. Commented Nov 14, 2018 at 9:21
  • What do you mean with this? this is Java, not Python. The python class syntax is a bit different then the java syntax stackoverflow.com/questions/64141/classes-in-python Commented Nov 14, 2018 at 9:22

1 Answer 1

2

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:

  1. As the first argument of your class methods def my_method(self, arg1, arg2):
  2. Within the class to refer to any other class members (just as demonstrated above) self.do_job("something", 123)
  3. 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.

Sign up to request clarification or add additional context in comments.

2 Comments

In your first example, you can simply use the keyword global instead of using a dictionary.
I don't think you should encourage OP to use self outside of a class. They already think it means something it doesn't (although it's not clear what). Use a different name.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.