Python Forum
Thread Rating:
  • 2 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Instances in classes
#2
Beginners mistake - both classes get copy of the same class variable SuperGrid.new, instead of new lists created per class.
Even worth - 10 * [10 * [0]]  - in Python, you usually put list on the left! - creates list of 10 references to innermost list

def __init__(self, square_size):
    self.square = [[0] * square_size for _ in range (square_size)]
will produce 10 individual lists of 10 individual sub-lists for each class - given that square_size is 10
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply


Messages In This Thread
Instances in classes - by dannyH - Apr-20-2017, 01:02 PM
RE: Instances in classes - by volcano63 - Apr-20-2017, 02:28 PM
RE: Instances in classes - by dannyH - Apr-20-2017, 03:01 PM
RE: Instances in classes - by volcano63 - Apr-20-2017, 03:07 PM
RE: Instances in classes - by dannyH - Apr-20-2017, 03:24 PM
RE: Instances in classes - by nilamo - Apr-20-2017, 03:49 PM
RE: Instances in classes - by dannyH - Apr-20-2017, 05:55 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
Question [solved] Classes, assign an attributes to a class not to instances.. SpongeB0B 4 1,017 May-20-2023, 04:08 PM
Last Post: SpongeB0B
  Using classes? Can I just use classes to structure code? muteboy 5 5,163 Nov-01-2017, 04:20 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

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