Class objects - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Class objects (/thread-29274.html) Pages:
1
2
|
Class objects - Python_User - Aug-25-2020 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. RE: Class objects - ndc85430 - Aug-25-2020 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.
RE: Class objects - jefsummers - Aug-25-2020 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()) RE: Class objects - bowlofred - Aug-25-2020 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 RE: Class objects - Python_User - Aug-26-2020 >>> 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 HelloThe 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
RE: Class objects - ndc85430 - Aug-26-2020 You need to override the special __str__ method to provide a string representation for the object.
RE: Class objects - snippsat - Aug-26-2020 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.
RE: Class objects - jefsummers - Aug-26-2020 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. RE: Class objects - Python_User - Aug-26-2020 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
RE: Class objects - buran - Aug-26-2020 (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 |