Python Forum

Full Version: Python basics about __init__ and return statement
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
hi all

I haven't used python in almost 4 years, and even back then I've only learned the very basic stuff (so basically forget it all..)
Could someone give me some hints on how my codes aren't working?


right now I'm just writing random functions to practise


class test:

    def __init__(self,x,y):
        '''Show doc'''
        self.x = x;
        self.y = y;


    def xySum():
        '''Sum'''
        return x+y
        
    def xyProduct():
        '''Product'''
        return x*y
        
    def xyDivision():
        return x/y

    def append_a(self,a):
        print 'when a arrives in append_a(), it looks', a
        a = a + ' and like that'
        print 'then it looks', a
        return a

    def create_a(self):
        a = 'like this'
        print 'a in create_a() looks', a
        a = append_a(a)
        print 'after coming back to create_a(), a looks', a
        product = xyProduct()
        print product

p = test(12,3)
p.create_a()
        
and here's the error

Error:
Traceback (most recent call last):   File "C:/Users/William/Documents/Python Scripts/test.py", line 36, in <module>     p.create_a()   File "C:/Users/William/Documents/Python Scripts/test.py", line 30, in create_a     a = append_a(a) NameError: global name 'append_a' is not defined
I think I'm still a bit confused about when to include self as an argument for a function, and how the return statement works..
I think it's the xySum, xyProduct..etc functions that I messed up?
Thanks in advance!

I've tried to include x,y in the arguments for those xySum,xyProduct functions too, but still didn't work :(
You need to use self..
(Oct-25-2016, 01:45 AM)micseydel Wrote: [ -> ]You need to use self..

yea..I should have. It's my first day trying to pick it up again haha
so now I added self to all of them, but still get this error?

class test:

    def __init__(self,x,y):
        '''Show doc'''
        self.x = x;
        self.y = y;


    def xySum(self,x,y):
        '''Sum'''
        return x+y
        
    def xyProduct(self,x,y):
        '''Product'''
        return x*y
        
    def xyDivision(self,x,y):
        return x/y

    def append_a(self,a):
        print 'when a arrives in append_a(), it looks', a
        a = a + ' and like that'
        print 'then it looks', a
        return a

    def create_a(self):
        a = 'like this'
        print 'a in create_a() looks', a
        a = self.append_a(a)
        print 'after coming back to create_a(), a looks', a
        product = xyProduct()
        print product

p = test(12,3)
p.create_a()
        
Error:
Traceback (most recent call last):   File "C:/Users/William/Documents/Python Scripts/test.py", line 36, in <module>     p.create_a()   File "C:/Users/William/Documents/Python Scripts/test.py", line 32, in create_a     product = xyProduct() NameError: global name 'xyProduct' is not defined
Does that mean it somehow can't find the xyProduct function?

wait..sorry I know I should have put arguments into xyProduct() inside create_a() since I defined it that way, but the same error still comes up
Same problem :)
thank you! I figured out :)...I feel so dumb
We've all been there, glad you figured it out!