-2

While trying to experiment with classes and methods, and how to pass variables between them, I wrote a couple of scripts to try to understand the mechanics. In doing so I hit an issue where one of my functions is un-defined:

NameError: name 'exclaim' is not defined

I thought use of self might resolve but i just loop round into

NameError: name 'self' is not defined

I've come across several sources regarding this leading me to look at indentation levels of methods, and calling via HelloWorld.exclaim() which hits the same issue.

please see my code: (script1)

import datasource

class HelloWorld:

    def exclaim():
        number1 = input("enter a number")
        datasource.Class2.method3.impvariable1 = number1

    def main():
        HelloWorld.exclaim()
        print(datasource.Class1.method1.variable1)
        print(datasource.Class2.method2.variable2)
        print(datasource.Class2.method3.variable3)

    if __name__ == '__main__':
        main()

Script2:

#datasource.py
class Class1:
    def method1():
        variable1 = "Hello "

class Class2:
    def method2():
        variable2 = "World"
    def method3():
        impvariable1 = 0
        variable3 = "!"
        for x in range(impvariable1):
            variable3 = variable3 + "!"

I also tried (amount 100's of other iterations)

    #datahandler.py
import datasource

class HelloWorld:

    def exclaim(self):
        number1 = input("enter a number")
        datasource.Class2.method3.impvariable1 = number1

def main(self):
    HelloWorld.exclaim(self)
    print(datasource.Class1.method1.variable1)
    print(datasource.Class2.method2.variable2)
    print(datasource.Class2.method3.variable3)

if __name__ == '__main__':
    main(self)

which produces;

NameError: name 'self' is not defined
Stefan A
  • 37
  • 1
  • 11
  • 4
    Instance methods should explicitly have `self` as their first parameter. Your `main` definition should not be inside a class. Your `if __name__` block should not be inside a class. I'm not even sure what your class is supposed to be for. – khelwood Feb 26 '19 at 11:43
  • 1
    Check out this link for a good guide on python OOP: https://realpython.com/python3-object-oriented-programming/ – liamhawkins Feb 26 '19 at 11:43
  • As it stands, I don't get the same error as you. Can you share the exact code giving this error? Or be clearer about what you are trying to do? Are you trying to set instance variables from another class (without an instance to hand)? – doctorlove Feb 26 '19 at 11:44
  • if condition and method main(0) need to write outside of HelloWorld class and every method inside a class need a self as first object like def exclaim(self): – ravishankar chavare Feb 26 '19 at 11:46
  • 1
    Literally followed every guide line, was very explicit and direct in what I needed. giving the shortest code necessary, and get grief and down voted because the code isn't clear :/ I'm a beginner newb, thats the problem, this forum is enough to scare people away from coding, Luckily Rahil was patient and kind enough to look at what the problem was objectively, you could all learn from him and make the programming community much nice and more accessible! – Stefan A Feb 26 '19 at 11:57
  • Stefan: You're getting the `NameError` in the final snippet because there's no `self` defined to pass as an argument to `main()`—which isn't even a `HelloWorld` method because it's not indented, so should even have a `self` parameter declared in its definition. – martineau Feb 26 '19 at 11:58

1 Answers1

2
import datasource

class HelloWorld:

    def exclaim(self):
        number1 = input("enter a number")
        datasource.Class2.method3.impvariable1 = number1

def main():
    obj = HelloWorld()
    obj.exclaim()
    print(datasource.Class1.method1.variable1)
    print(datasource.Class2.method2.variable2)
    print(datasource.Class2.method3.variable3)

if __name__ == '__main__':
    main()
Rahil Hastu
  • 558
  • 2
  • 13
  • This is the solution!! thank you ever so much, given the beating i was getting from people needing more information, I cant believe you did it so quickly - Amazing Thanks very much!! – Stefan A Feb 26 '19 at 11:52