Python Forum

Full Version: Class objects
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Hi all,

I can't place the following 'thing' in Python coding.

Why is the output of print (MyClass.f) in the code below, looks like this: <function MyClass.f at 0x01DDE190>?

class MyClass:
    i = 12345

    def f(self):
        return 'hello world'

print (MyClass.i)
print (MyClass.f)
Thanks.
Because f is a function. What did you expect? If you expected to see hello world, you actually need to call the function by putting parentheses on the end.
Because you are not actually calling the function. Put a pair of parentheses after the f in the print statement and it will call it. You are getting exactly what you are asking for

Try
print(MyClass.f())
That's the default representation of a function when printed (as opposed to when the function is executed). It's very similar for functions outside of a class as well. Are you expecting some other type of output?

>>> def myfunc():
...   return("Hello")
...
>>> print(myfunc)      # No parenthesis after the name.  We are referring to the object
<function myfunc at 0x105f07620>
>>> print(myfunc())    # Parenthesis after the name.  We are calling the object as a function
Hello
>>> def myfunc():
...   return("Hello")
...
>>> print(myfunc)      # No parenthesis after the name.  We are referring to the object
<function myfunc at 0x105f07620>
>>> print(myfunc())    # Parenthesis after the name.  We are calling the object as a function
Hello
The code above makes sense, I do understand it know. Knowing this information, I thought about the following class and having the same issue and doesn't get the clue.... See remarks in the code below as well.

class Game:
    def __init__ (self, title, year, price):
        self.title = title
        self.year = year
        self.price = price

g1 = Game("FIFA", 2008, "€50")

print (g1)              # why is this again refer to an object number?? I was expecting as output: 
                        # FIFA, 2008, €50 --> so all the arguments which are in the 'Game' class


print (g1.price)        # this line works properly and I do understand how the code is executed
print (g1.year)
Thanks

In addition to the last answer,

I try to 'follow' the code and visualize it, but I am getting stuck when the code reach the class Game
You need to override the special __str__ method to provide a string representation for the object.
As mention over __str__ and can also add __repr__.
class Game:
    def __init__ (self, title, year, price):
        self.title = title
        self.year = year
        self.price = price

    def __str__(self):
        return f'Title:{self.title} Year:{self.year} Price:{self.price}'

    def __repr__(self):
        return f'Game({self.title}, {self.year}, {self.price})'
Usage.
>>> g1 = Game("FIFA", 2008, "€50")
>>> 
>>> # Call __str__
>>> print(g1)
Title:FIFA Year:2008 Price:€50
>>> 
>>> # Call __repr__
>>> g1
Game(FIFA, 2008, €50)
__str__ should primarily be readable,and something you feel comfortable displaying to a user.
__repr__ are helpful for developers,here show how to recreate the original g1 object.
Adding to ndc85430 - think about how you would expect the class to respond if you had a few functions in the class as well. __str__ allows you to customize the output when you print the class.
Thanks for the quick responses :)

But I am actually seeking for the reason of why this code is displaying the object number and not how I can show the print (g1) as FIFA, 2008, €50
(Aug-26-2020, 07:16 PM)Python_User Wrote: [ -> ]But I am actually seeking for the reason of why this code is displaying the object number
(Aug-25-2020, 07:40 PM)bowlofred Wrote: [ -> ]That's the default representation of a function when printed (as opposed to when the function is executed).

you didn't implement __repr__ or __str__ method, so that's what you get
Pages: 1 2