Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8405553
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:52:53+00:00 2026-06-09T22:52:53+00:00

‘sup guys, I’m just doing my usual random little programs before my exam within

  • 0

‘sup guys,

I’m just doing my usual random little programs before my exam within a few days and I decided to write a five card draw poker. Since it’s simply for playing a bit with code, I didn’t inherit anything from basic classes. My deck contains a list attribute, which has been given the functionality it needs for simple readability and functionality:

import random

from card import Card

class Deck():

    def __init__(self):
        self.cards = self.__generate_cards()
        random.shuffle(self.cards)

    def __str__(self):
        cards_str = ""
        for card in self.cards:
            cards_str += str(card)
        return cards_str

    def __getitem__(self,index):
        return self.cards[index]

    def __generate_cards(self):
        cards = []
        for i in range(4):
            for j in range(13):
                cards.append(Card(i,j+1))
        return cards

    def pop(self,index):
        return self.cards.pop(index)

as you can see, there is no .__iter__ method to be found, which means I can’t iterate over a Deck() object right? Python shows me a different story: for some reason the next code results in looping over the card objects in self.cards and actually printing the cards without the need of an iterator:

deck = Deck()
for card in deck:
    print(card)    # cards have a __str__ method

Doesn’t make much sense to me, unless it’s part of the python magic, giving any object a chance for possible looping. ;p
Can anyone show me the light with this one? thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T22:52:54+00:00Added an answer on June 9, 2026 at 10:52 pm

    Why does defining __getitem__ on a class make it iterable in python?

    It’s a back-compat thing. Having __getitem__ makes it iterable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I am doing a simple coin flipping experiment for class that involves flipping a
I would like to count the length of a string with PHP. The string
I was writing code for dragging mechanism which invokes to wait for small period
I've got a string that has curly quotes in it. I'd like to replace
I downloaded PostgreSQL from the official website and ran the .dmg installer. After that
This is how I get the tags of a body of text. var tags
I'm running a Octopress blog which is based on Jekyll . Now I wanted
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I would like to run a str_replace or preg_replace which looks for certain words

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.