Python Forum
Thread Rating:
  • 2 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Instances in classes
#6
1) You don't need backslashes between lines.
2) Instead of class-level init-variables, why not have all that in __init__, and have the default value be None and use the default values if nothing is passed?  That way you never have to deal with shared references, since the variables simply never exist outside of an instance.

Something like:
class gridSquare(object):
   def __init__(self, size = None, square = None):
       if square is None:
           square = [
                ['0',' ','F','O','W','E','K','T','A','H'],
                ['1','A','G','O','X','E','L','U','B','I'],
                ['2','A','H','P','Y','E','M','U','C','I'],
                ['3','B','I','Q','Z','E','N','V','D','J'],
                ['4','C','I','R',' ','F','O','W','E','K'],
                ['5','D','J','S','A','G','O','X','E','L'],
                ['6','E','K','T','A','H','P','Y','E','M'],
                ['7','E','L','U','B','I','Q','Z','E','N'],
                ['8','E','M','U','C','I','R',' ','F','O'],
                ['9','E','N','V','D','J','S','A','G','O']
           ]
       if size is None:
           size = len(square)
       self.square =  square
       self.size = size

grid = gridSquare()
print(grid.size) # 10
print(grid.square) # ...the grid
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,076 May-20-2023, 04:08 PM
Last Post: SpongeB0B
  Using classes? Can I just use classes to structure code? muteboy 5 5,229 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