Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Class objects
#1
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.
Reply
#2
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.
Reply
#3
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())
Reply
#4
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
Reply
#5
>>> 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
Reply
#6
You need to override the special __str__ method to provide a string representation for the object.
Reply
#7
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.
Reply
#8
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.
Reply
#9
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
Reply
#10
(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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How can I access objects or widgets from one class in another class? Konstantin23 3 997 Aug-05-2023, 08:13 PM
Last Post: Konstantin23
  How to create and define in one line a 2D list of class objects in Python T2ioTD 1 2,033 Aug-14-2020, 12:37 PM
Last Post: Yoriz
  printing class properties from numpy.ndarrays of objects pjfarley3 2 1,949 Jun-08-2020, 05:30 AM
Last Post: pjfarley3
  Help with lists and class objects AlluminumFoil 15 5,488 Jan-15-2020, 07:32 PM
Last Post: AlluminumFoil
  How to serialize custom class objects in JSON? Exsul1 4 3,490 Sep-23-2019, 08:27 AM
Last Post: wavic
  How do I write class objects to a file in binary mode? Exsul1 7 5,740 Sep-14-2019, 09:33 PM
Last Post: snippsat
  Do objects get their own copy of the class methods? Charles1 1 2,089 Feb-02-2019, 04:40 PM
Last Post: ichabod801
  AttributeError: 'NoneType' object has no attribute 'n' in list of class objects jdrp 4 5,733 Jun-19-2018, 02:44 PM
Last Post: jdrp
  How to check if class instance exists in a list of class instance objects? sonicblind 23 20,296 May-27-2018, 05:44 AM
Last Post: buran
  Class Objects... zowhair 2 2,981 Mar-10-2018, 08:01 AM
Last Post: wavic

Forum Jump:

User Panel Messages

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