Python Forum
Python basics about __init__ and return statement
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python basics about __init__ and return statement
#1
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 :(
Reply
#2
You need to use self..
Reply
#3
(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
Reply
#4
Same problem :)
Reply
#5
thank you! I figured out :)...I feel so dumb
Reply
#6
We've all been there, glad you figured it out!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Need help with Return statement Columbo 13 2,179 Sep-17-2022, 04:03 PM
Last Post: Columbo
  Python class doesn't invoke setter during __init__, not sure if's not supposed to? mtldvl 2 3,245 Dec-30-2021, 04:01 PM
Last Post: mtldvl
  Learned Python Basics, what now? muzikman 20 5,859 Oct-11-2021, 01:34 AM
Last Post: Underscore
  How to invoke a function with return statement in list comprehension? maiya 4 2,752 Jul-17-2021, 04:30 PM
Last Post: maiya
  syntax error on return statement l_butler 5 3,027 May-31-2020, 02:26 PM
Last Post: pyzyx3qwerty
  PYserial basics? bako 10 5,025 Apr-26-2020, 09:15 PM
Last Post: bowlofred
  [split] Debugging scripts basics tultalk 5 3,018 Apr-25-2020, 01:02 AM
Last Post: menator01
  Debugging scripts basics bako 8 3,420 Apr-24-2020, 06:18 AM
Last Post: Larz60+
  return statement will not work TheTechRobo 2 2,588 Mar-30-2020, 06:22 PM
Last Post: TheTechRobo
  HELP! Return Statement Debugging HappyMan 5 3,060 Jan-27-2020, 07:31 PM
Last Post: michael1789

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020