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

    Join Date
    Jan 2013
    Posts
    78
    Rep Power
    2

    More questions about classes/objects


    Hey, I just have a couple more questions about classes/objects..

    To get a to return self.value when called, like b does. What method do I have to add to test1?

    Why does the two last print statements return different texts when i print them? and is there a value i could change or method i could add to make them return something else?

    How do I find the standard variable name(which it inherited from the int class or one of its parents) in test2, so I can alter it/read it/ do anything with it? (ye, I know I could just call the object and it would return it to me)

    Code:
    #!/usr/bin/python
    
    class test1:
        def __init__(self, Value):
    	self.Value = Value
    class test2(int):
        pass
    
    a = test1(5)
    b = test2(10)
    
    print(a)
    print(b)
    print(" ")
    print(test1)
    print(test2)
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2007
    Location
    Joensuu, Finland
    Posts
    431
    Rep Power
    67
    Originally Posted by leonnaley2
    To get a to return self.value when called, like b does. What method do I have to add to test1?
    I’m not sure I understand... Your variable “b” is an instance of class test2 which is a subclass of int. Neither test2 nor int defines self.value so I’m not sure what you mean by “like b does”.

    Why does the two last print statements return different texts when i print them?
    Neither class defines __repr__() or __str__() functions so Python reverts to its default action when the object is printed. test2 is a subclass of int, and int objects return the “self” value when printed. test1 is a subclass of object, and when printed these usually show only information about the object.

    How do I find the standard variable name(which it inherited from the int class or one of its parents) in test2, so I can alter it/read it/ do anything with it?
    There are no “standard variable names”. Maybe you mean “self”? Or you could do “dir(test1)”.

    In any case, maybe you should just define __repr__():

    Code:
    def Test1(object):
        def __init__(self, value):
            self.value = value
        def __repr__(self):
            return str(self.value)
    (Note: it’s customary to Capitalize class names and use lowercase for functions and variables.)
    My armada: openSUSE 13.1 (home desktop, home laptop), Crunchbang Linux 11 (mini laptop, work laptop), Android 4.2.1 (tablet)
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    78
    Rep Power
    2
    Thanks, that helped a bit. Implementing __repr__ in my class fixed the first print issue. Sorry for the bad questioning.

    How would I make the last two print statements work/print out the same things then? (without subclassing int?)

    Code:
    #!/usr/bin/python
    
    class test():
        def __init__(self, Value):
    	self.Value = Value
        def __repr__(self):
    	return(str(self.Value))
        def __add__(self, OtherValue):
    	return(self.Value+OtherValue)
    
    a = test(5)
    b = int(5)
    
    print(a)
    print(b)
    
    print(a+1)
    print(b+1)
    
    print(int)
    print(test)
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2007
    Location
    Joensuu, Finland
    Posts
    431
    Rep Power
    67
    Originally Posted by leonnaley2
    How would I make the last two print statements work/print out the same things then? (without subclassing int?)
    Do you mean these two:

    Code:
    print(int)
    print(test)
    I can’t figure out why you would want them to “print out the same thing” – wouldn’t that be just lying? The “test” object is not an int and it shouldn’t pretend it is. Actually, I can’t think of any reason to print types in this way in the first place!
    My armada: openSUSE 13.1 (home desktop, home laptop), Crunchbang Linux 11 (mini laptop, work laptop), Android 4.2.1 (tablet)

IMN logo majestic logo threadwatch logo seochat tools logo