Python Forum
TypeError: 'Subscription' object is not iterable in Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
TypeError: 'Subscription' object is not iterable in Python
#1
I have a list like this:
lst = [(9600002, 42, 3), (9600001, 17, 3), (9600003, 11, 1),
   (9600002, 14, 5), (9600001, 17, 1), (9600003, 11, 4),
   (9600001, 17, 4), (9600001, 14, 3), (9600002, 42, 6),
   (9600002, 42, 1)]
every tuple belongs to another class called Subscription
class Subscription:
    def __init__(self, user_id, program_code, season_id):
        self._user_id = user_id
        self._program_code = program_code
        self._season_id = season_id
    def get_user_id(self):
        return self._user_id
    def get_program_code(self):
        return self._program_code
    def get_season_id(self):
        return self._season_id
    def __repr__(self):
      return '(' + str(self._user_id) + ', ' + str(self._program_code) + ', ' + str(self._season_id) + ')'
Below is what i am doing wrong. I am trying to use Counter

This is what i got: ''TypeError: 'Subscription' object is not iterable''

from collections import Counter
from subscription import Subscription

def main():
    lst = load_subscriptions()
    c = Counter()                           #this is where i got the error
    for user, program, season in lst:      #this is where i got the error
        c[(user, program)] += 1             #this is where i got the error

def load_subscriptions():
    from sys import stdin
    lst = []
    for line in stdin:
        user_id, program_code, season_id = (int(x) for x in line.split())
        if program_code != 42:
            lst.append(Subscription(user_id, program_code, season_id))
    return lst

if __name__ == "__main__":
    main()
Then how could get this problem fixed? Any help would be appreciated! Smile Smile Smile
Reply
#2
(Apr-09-2018, 09:47 AM)hfan6206 Wrote:
for user, program, season in lst: 
Ok, so if you want deconstruction to work like that, you need to do a little more work with the Subscription class, so Python knows how you want it deconstructed.

The easy way, would be to just not deconstruct it:
for sub in lst:
    user = sub._user_id
Reply
#3
Cool,thx
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  TypeError: cannot pickle ‘_asyncio.Future’ object Abdul_Rafey 1 273 Mar-07-2024, 03:40 PM
Last Post: deanhystad
  error in class: TypeError: 'str' object is not callable akbarza 2 456 Dec-30-2023, 04:35 PM
Last Post: deanhystad
Bug TypeError: 'NoneType' object is not subscriptable TheLummen 4 679 Nov-27-2023, 11:34 AM
Last Post: TheLummen
  TypeError: 'NoneType' object is not callable akbarza 4 921 Aug-24-2023, 05:14 PM
Last Post: snippsat
  [NEW CODER] TypeError: Object is not callable iwantyoursec 5 1,262 Aug-23-2023, 06:21 PM
Last Post: deanhystad
  TypeError: 'float' object is not callable #1 isdito2001 1 1,046 Jan-21-2023, 12:43 AM
Last Post: Yoriz
  TypeError: a bytes-like object is required ZeroX 13 3,839 Jan-07-2023, 07:02 PM
Last Post: deanhystad
  TypeError: 'float' object is not callable TimofeyKolpakov 3 1,375 Dec-04-2022, 04:58 PM
Last Post: TimofeyKolpakov
  API Post issue "TypeError: 'str' object is not callable" makeeley 2 1,834 Oct-30-2022, 12:53 PM
Last Post: makeeley
  TypeError: 'NoneType' object is not subscriptable syafiq14 3 5,169 Sep-19-2022, 02:43 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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