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
#5
Quote:You need to show the complete code that doesn't work.

In general, you can declare variables (also called fields) in classes that are "local" to that class. In your examples, the Sur class in class0.py declares a result field and initializes it as a list:

I'm just not able to conceptualize list in oop. I get the codes to work (by debugging & trial/error) but the logic confuses me...especially with list in constructor methods...ijdu

for example this code works...I'm faithful to keeping each function simple and doing 1 single task. please see code comments for my questions pertaining to the [].
#class.py
import random

class Die():
	def __init__(self,sides=6):
        #here are my instance variables(object characteristics come from these) AKA attributes/fields
        #[] is treated as an attribute(field) Why?  it should not be but it is
        #why? (obviously I can memorize this as an exception to the rule (lists and fields in __init__) it's fine...i just don't understand the why...
		self.sides = sides
		self.throws=[]

	def roll(self,turn):#turn field doesn't 'exist' prior to this method.
		#so no pt in defining in constructor method...I sort of understand this...it's fine
		self.turn = random.randint(1,self.sides)#random is inclusive, range is excl. pretty annoying
		return self.turn
	def update_rolls(self):
		self.throws.append(self.turn)
		return self.throws
	def print_list(self):
		for t in self.throws:
			print('die is -',t)
#run.py

from class import Die

die = Die()
results=[]#if I delete this I get an attribute error that die obj has no attribute named update_rolls.
#update_rolls is a method not an attribute.It just uses the [] attribute

for i in range(1,3):
	t=die.roll(i)
	die.update_rolls() #so this is a func...clearly...but if I delete results =[] python thinks update_rolls() is intended as attribute?
die.print_list()
Maybe I'm overthinking this but why is my list a field (in __init__)? Is it because my other methods use it? If I create all class variables in the constructor method why can I create the variable 'turn' in the roll() method? Basically I am forced to create list[] in __init__ but all other fields through any function? Very confused...
Reply


Messages In This Thread
RE: understanding lists....I'm very confused - by mepyyeti - Dec-23-2017, 02:38 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  String int confused janeik 7 1,107 Aug-02-2023, 01:26 AM
Last Post: deanhystad
  I am confused with the key and value thing james1019 3 995 Feb-22-2023, 10:43 PM
Last Post: deanhystad
  Pandas confused DPaul 6 2,621 Sep-19-2021, 06:45 AM
Last Post: DPaul
  is and '==' i'm confused hshivaraj 6 2,758 Sep-15-2021, 09:45 AM
Last Post: snippsat
  Confused with 'flags' tester_V 10 4,968 Apr-12-2021, 03:03 AM
Last Post: tester_V
  Simple Tic Tac Toe but I'm confused Izith 1 2,225 Sep-26-2020, 04:42 PM
Last Post: Larz60+
  Split dict of lists into smaller dicts of lists. pcs3rd 3 2,434 Sep-19-2020, 09:12 AM
Last Post: ibreeden
  I am really confused with this error. Runar 3 3,053 Sep-14-2020, 09:27 AM
Last Post: buran
  Confused on how to go about writing this or doing this... pythonforumuser 3 2,515 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,318 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