Python Forum
understanding lists....I'm very confused
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
understanding lists....I'm very confused
#1


I'm trying to get a grasp on how lists behave. I've included two VERY simple .py programs. I'm trying to get comfortable using oop (I can do blunt code...but oop trips me up...)

my question is can anyone explain how is python treating the lists? In one program I instantiate the list in the class...in the second program I do not. The only thing I can think of is I need to instantiate in class0.py __init__ func when the list is such that it will take in user's input? idk if this explanation is correct

I've better elaborated my question in the code comment...please help. Thx && HAPPY HOLIDAYS!!!!!

#class0.py
class Sur():
	def __init__(self):
		self.things = [] #pls note list is instantiated...but in class1.py I don't use it?
	def add_info(self,new_ish):
		self.things.append(new_ish)
		print('added:',new_ish)
		return new_ish
	def print_all(self):
		for j in self.things:
			print(j)

#run0.py
from class0 import Sur
Surv=Sur()
listt=[] #instantiated here as well since list exists in __init__
for i in range(1,2):
	incoming = str(input('> '))
	if incoming == 'q' or incoming =='':
		break
	else:
		Surv.add_info(incoming)
Surv.print_all()
#class1.py
import random
class Die():
	def __init__(self,sides=6):
		self.sides = sides
        #notice there is no instantiation of list in this file 
        #QUESTION IS WHY IS THIS????

	def roll(self,turn):
		self.turn = random.randint(1,self.sides)
		return self.turn		
#run1.py
from class1 import Die
foo =Die()
results = []#why isn't there a list in class1  ---IDU this!!!

for i in range(1,2):
	drop = foo.roll(i)
	results.append(drop)
	print(str(i)+']'+str(drop))
Reply


Messages In This Thread
understanding lists....I'm very confused - by mepyyeti - Dec-22-2017, 11:04 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  String int confused janeik 7 1,085 Aug-02-2023, 01:26 AM
Last Post: deanhystad
  I am confused with the key and value thing james1019 3 972 Feb-22-2023, 10:43 PM
Last Post: deanhystad
  Pandas confused DPaul 6 2,580 Sep-19-2021, 06:45 AM
Last Post: DPaul
  is and '==' i'm confused hshivaraj 6 2,725 Sep-15-2021, 09:45 AM
Last Post: snippsat
  Confused with 'flags' tester_V 10 4,939 Apr-12-2021, 03:03 AM
Last Post: tester_V
  Simple Tic Tac Toe but I'm confused Izith 1 2,203 Sep-26-2020, 04:42 PM
Last Post: Larz60+
  Split dict of lists into smaller dicts of lists. pcs3rd 3 2,386 Sep-19-2020, 09:12 AM
Last Post: ibreeden
  I am really confused with this error. Runar 3 3,038 Sep-14-2020, 09:27 AM
Last Post: buran
  Confused on how to go about writing this or doing this... pythonforumuser 3 2,501 Feb-10-2020, 09:15 AM
Last Post: snippsat
  sort lists of lists with multiple criteria: similar values need to be treated equal stillsen 2 3,291 Mar-20-2019, 08:01 PM
Last Post: stillsen

Forum Jump:

User Panel Messages

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