#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2005
    Posts
    7
    Rep Power
    0

    Anothe Newbie question about a class problem..


    Hey all, (its probably a stupid question) I have this problem with a module i'm trying to make to use inside another program, and whenever i try and run it or import it at the IDLE main window, i get a type error, and it tells me i've got an "unbound method" which is screwing everything up. I looked it up on the net, but i still don't understand the difference between unbound and bound methods.
    here is the code:
    Code:
    class PerfTest:
        "Tests for Perfect Squares"
        def __init__(self):
            self.number = 'Test a Number'
        def Test(self, number):
            from math import sqrt
            root1 = sqrt(number)
            if number % root1 == 0:
                return(number),
                return(" is a Perfect Square")
            else:
                return(number),
                return(" is not a Perfect Square")
    If someone could point me in the direction of something that explains the differences in an understandable way, and possibly what i can do to change the code, please let me know!
  2. #2
  3. No Profile Picture
    Javascript is the debbel
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    102
    Rep Power
    12
    when you're defining a class you're more describing what a type of object looks like. To create an instance of it you need to run it as such:

    Code:
    PerfTestInstance = PerfTest()
    after which you can use PerfTestInstance.Test(1) or whatever to actually run the "Test" method.


    also a sidenote, I don't mean to be rude but it's usually more annoying to hear a person continually apologize about what they don't know, or how much of a newb they are, we're more interested in how we can help you.

    sorry for being so direct, but it cuts a lot of time out reading the same "I'm sorry" 's over-and-over again.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    88
    Rep Power
    11
    This is what you should do:

    >>>import PerfTest (or whatever you named the file that you put that class into)
    >>>method = PerfTest()
    >>>method.test(4)
    4 is a perfect square
    >>>
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2005
    Posts
    7
    Rep Power
    0
    thanks very much - i'll try not to be so apologetic! (thats sort of an apology isn't it? screw it...)
  8. #5
  9. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    You can only have one return statement per use in each function. Doing something such as
    Code:
    return '1'
    return '2'
    at the same time will result in only returning '1'.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    11
    well you can have more then one:

    Code:
    >>> def Tests(x):
    	if x < 5:
    		return "Smaller than 5"
    	else:
    		return "Greater than 5 or 5"
    
    	
    >>> Tests(5)
    'Greater than 5 or 5'
    Those people who think they know everything are a great annoyance to those of us who do.
  12. #7
  13. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    Originally Posted by monkeyman23555
    well you can have more then one:

    Code:
    >>> def Tests(x):
    	if x < 5:
    		return "Smaller than 5"
    	else:
    		return "Greater than 5 or 5"
    
    	
    >>> Tests(5)
    'Greater than 5 or 5'
    Monkeyman, your post prooved nothing. Re-read my post.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    11
    ya but the first sentence...

    It says you can only have one return statement, unless I am misunderstanding you which I don't want to

    Sorry if I did
    Those people who think they know everything are a great annoyance to those of us who do.
  16. #9
  17. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    My first sentence makes more sense when you put the secong sentence with it. I guess I didn't explain enough by what I meant when I said "per use". I meant for each time you want to return something in a function, you can (should) only use one return statement.

IMN logo majestic logo threadwatch logo seochat tools logo