Python Forum
How to write a code to get this kind of output in IDLE
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to write a code to get this kind of output in IDLE
#1
In IDLE I imported sympy.matrices module

for an example
I entered the following
>>> from sympy.matrices import Matrix
>>> a=Matrix([[1,2,3],[4,5,6],[7,8,9]])
>>> a
Matrix([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
and also I imported a custom library created by me.
this is a part of my module
class dog:
    def __init__(self,name,age,gender):
        self.name=name
        self.age=age
        self.gender=gender
In IDLE I entered following in the same as the previous
>>> from my_modules.dog import dog
>>> b=dog("sam",5,"male")
>>> b
<my_modules.dog.dog object at 0x00460950>
the problem is I entered codes in the same way. But the outputs are in the different forms

in first example it gave the following output instead of giving an output
just like <sympy.matrices.Matrix object at 0x00512950>
Output:
Matrix([ [1, 2, 3], [4, 5, 6], [7, 8, 9]])
I need to know how to code my custom module to return an output just like
>>>b
Dog('sam','5years','male')
instead of <my_modules.dog.dog object at 0x00460950>

can someone help Me please Pray Pray
Reply
#2
Looks like your almost there.

I think you need to be more specific on your last line, try putting the following into the shell:

b.name + ", " + b.age + ", " + b.gender
Reply
#3
No Mister Sad

I don't mean that.

after creating the "Dog" object "b", I entered this in IDLE
>>> b
It returns the object type of the object "b" and the ID of the object.

But in the first example I created the "Matrix" object "a".
By entering
>>> a
It returns the Matrix instead of it's type and ID Shocked

I'm asking for the method used in Matrix object Think Doh

I'm not talking about the print function or something like that
Reply
#4
Your class is missing __repr__ method.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
You need to implement a __repr__ as mention bye @wavic,can also trow in a __str__ method.
class Dog:
    def __init__(self, name, age, gender):
        self.name = name
        self.age = age
        self.gender = gender

    def __str__(self):
        return f'Name: {self.name} Age: {self.age}-year and Gender: {self.gender}'

    def __repr__(self):
        return f'Name: {self.name} Age: {self.age}-year and Gender: {self.gender}'
Output:
>>> from my_module import Dog >>> >>> obj = Dog('Sam', 5, 'Male') >>> obj # call __repr__ Name: Sam Age: 5-year and Gender: Male >>> >>> print(obj) # call __str__ Name: Sam Age: 5-year and Gender: Male
Without the obj will look like this.
>>> from my_module import Dog
>>> 
>>> obj = Dog('Sam', 5, 'Male')
>>> obj
<my_module.Dog object at 0x055AD110>
>>> print(obj)
<my_module.Dog object at 0x055AD110>
>>> 
>>> obj.name # This will work in both examples
'Sam'
>>> obj.age
5
>>> obj.gender
'Male'
Reply
#6
Usually the __repr__ method returns the what you run to create the instance of that class.

def __repr__(self):
    return f'Dog({self.name}, {self.age}, {self.gender})'
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  write code that resides in parent directory franklin97355 3 374 Apr-14-2024, 02:03 AM
Last Post: franklin97355
  Why can I not see the code correctly in Python IDLE.? Trump 8 669 Apr-04-2024, 07:47 AM
Last Post: jonesphedra
  problem in output of a snippet code akbarza 2 358 Feb-28-2024, 07:15 PM
Last Post: deanhystad
  needing some help to write some code for a game calculator rymdaksel 1 404 Jan-02-2024, 09:56 AM
Last Post: deanhystad
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,091 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  I cannot able to see output of this code ted 1 751 Feb-22-2023, 09:43 PM
Last Post: deanhystad
  What kind of list is this? jesse68 2 1,131 Jun-29-2022, 05:02 PM
Last Post: rob101
  why I dont get any output from this code William369 2 1,123 Jun-23-2022, 09:18 PM
Last Post: William369
  How can I organize my code according to output that I want ilknurg 1 1,167 Mar-11-2022, 09:24 AM
Last Post: perfringo
Photo how I write the output into xml file in python? 3lnyn0 1 2,225 Oct-31-2021, 05:40 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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