Alireza1999
Alireza1999

Reputation: 53

How do I print the object contents instead of their memory location?

I have video_library.py module with the class VideoLibrary:

class VideoLibrary: """A class used to represent a Video Library."""

def __init__(self):
    """The VideoLibrary class is initialized."""
    self._videos = {}
    with open(Path(__file__).parent / "videos.txt") as video_file:
        reader = _csv_reader_with_strip(
            csv.reader(video_file, delimiter="|"))
        for video_info in reader:
            title, url, tags = video_info
            self._videos[url] = Video(
                title,
                url,
                [tag.strip() for tag in tags.split(",")] if tags else [],
            )

def get_all_videos(self):
    """Returns all available video information from the video library."""
    return list(self._videos.values())

def get_video(self, video_id):
    """Returns the video object (title, url, tags) from the video library.

    Args:
        video_id: The video url.

    Returns:
        The Video object for the requested video_id. None if the video
        does not exist.
    """
    return self._videos.get(video_id, None)

I am trying to print the objects from get_all_videos() from my video_library.py module but its printing the object location instead of the list: //This is my video_player.py module

Class VideoPlayer:
    def show_all_videos(self):
        """Returns all videos."""
        all_vids = self._video_library.get_all_videos()

        for i in all_vids:
            print(i)

the outcome:

<video.Video object at 0x000001DCED0539D0>
<video.Video object at 0x000001DCED0538E0>
<video.Video object at 0x000001DCED2386A0>
<video.Video object at 0x000001DCED3A12E0>
<video.Video object at 0x000001DCED3A1CA0>

what I want:

Funny Dogs | funny_dogs_video_id |  #dog , #animal
Amazing Cats | amazing_cats_video_id |  #cat , #animal
Another Cat Video | another_cat_video_id |  #cat , #animal
Life at Google | life_at_google_video_id |  #google , #career
Video about nothing | nothing_video_id |

Upvotes: 1

Views: 221

Answers (1)

brunns
brunns

Reputation: 2764

Add a __repr__ method to Video:

    def __repr__(self):
        return f"{self.title}, {self.url}, {self.tags}"

(Guessing here a little, since you don't show the Video class definition.)

Upvotes: 1

Related Questions