Using self and parameters in a Python Script [closed]












-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?










share|improve this question













closed as unclear what you're asking by timgeb, Mike Scotty, mehrdad-pedramfar, EdChum, gnat Nov 14 '18 at 9:32


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.















  • What is self in main?
    – mehrdad-pedramfar
    Nov 14 '18 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.
    – quant
    Nov 14 '18 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?
    – Qohelet
    Nov 14 '18 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.
    – Mike Scotty
    Nov 14 '18 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
    – quant
    Nov 14 '18 at 9:22
















-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?










share|improve this question













closed as unclear what you're asking by timgeb, Mike Scotty, mehrdad-pedramfar, EdChum, gnat Nov 14 '18 at 9:32


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.















  • What is self in main?
    – mehrdad-pedramfar
    Nov 14 '18 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.
    – quant
    Nov 14 '18 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?
    – Qohelet
    Nov 14 '18 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.
    – Mike Scotty
    Nov 14 '18 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
    – quant
    Nov 14 '18 at 9:22














-5












-5








-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?










share|improve this question













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?







python python-3.x






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 9:17









Qohelet

3961623




3961623




closed as unclear what you're asking by timgeb, Mike Scotty, mehrdad-pedramfar, EdChum, gnat Nov 14 '18 at 9:32


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.






closed as unclear what you're asking by timgeb, Mike Scotty, mehrdad-pedramfar, EdChum, gnat Nov 14 '18 at 9:32


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.














  • What is self in main?
    – mehrdad-pedramfar
    Nov 14 '18 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.
    – quant
    Nov 14 '18 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?
    – Qohelet
    Nov 14 '18 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.
    – Mike Scotty
    Nov 14 '18 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
    – quant
    Nov 14 '18 at 9:22


















  • What is self in main?
    – mehrdad-pedramfar
    Nov 14 '18 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.
    – quant
    Nov 14 '18 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?
    – Qohelet
    Nov 14 '18 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.
    – Mike Scotty
    Nov 14 '18 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
    – quant
    Nov 14 '18 at 9:22
















What is self in main?
– mehrdad-pedramfar
Nov 14 '18 at 9:18




What is self in main?
– mehrdad-pedramfar
Nov 14 '18 at 9:18




3




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.
– quant
Nov 14 '18 at 9:19






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.
– quant
Nov 14 '18 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?
– Qohelet
Nov 14 '18 at 9:20




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?
– Qohelet
Nov 14 '18 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.
– Mike Scotty
Nov 14 '18 at 9:21






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.
– Mike Scotty
Nov 14 '18 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
– quant
Nov 14 '18 at 9:22




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
– quant
Nov 14 '18 at 9:22












1 Answer
1






active

oldest

votes


















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.






share|improve this answer























  • In your first example, you can simply use the keyword global instead of using a dictionary.
    – Corentin Limier
    Nov 14 '18 at 9:25










  • 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.
    – Daniel Roseman
    Nov 14 '18 at 9:39




















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









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.






share|improve this answer























  • In your first example, you can simply use the keyword global instead of using a dictionary.
    – Corentin Limier
    Nov 14 '18 at 9:25










  • 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.
    – Daniel Roseman
    Nov 14 '18 at 9:39


















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.






share|improve this answer























  • In your first example, you can simply use the keyword global instead of using a dictionary.
    – Corentin Limier
    Nov 14 '18 at 9:25










  • 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.
    – Daniel Roseman
    Nov 14 '18 at 9:39
















2












2








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.






share|improve this answer














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.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 14 '18 at 9:52

























answered Nov 14 '18 at 9:23









Mercury

2,6282027




2,6282027












  • In your first example, you can simply use the keyword global instead of using a dictionary.
    – Corentin Limier
    Nov 14 '18 at 9:25










  • 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.
    – Daniel Roseman
    Nov 14 '18 at 9:39




















  • In your first example, you can simply use the keyword global instead of using a dictionary.
    – Corentin Limier
    Nov 14 '18 at 9:25










  • 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.
    – Daniel Roseman
    Nov 14 '18 at 9:39


















In your first example, you can simply use the keyword global instead of using a dictionary.
– Corentin Limier
Nov 14 '18 at 9:25




In your first example, you can simply use the keyword global instead of using a dictionary.
– Corentin Limier
Nov 14 '18 at 9:25












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.
– Daniel Roseman
Nov 14 '18 at 9:39






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.
– Daniel Roseman
Nov 14 '18 at 9:39





Popular posts from this blog

How to pass form data using jquery Ajax to insert data in database?

National Museum of Racing and Hall of Fame

Guess what letter conforming each word