First off, don't call any file "class.py" that's just going to cause you all sorts of problems.
(Dec-23-2017, 02:38 AM)mepyyeti Wrote: [ -> ]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)
You say, "[] is treated as an attribute". This is incorrect. self.throws is the attribute; you are simply initializing it to be an empty list. To put it another way, self.throws is an attribute of type "list".
Quote:#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
This is simply not true. I can say that without running the code, but I also ran the code with and without the results list defined and it works either way.
Again, don't name any file "class.py". "class" is a keyword in Python and saying "from class import X" doesn't work.
Quote:Maybe I'm overthinking this but why is my list a field (in __init__)? Is it because my other methods use it?
Exactly. It's good programing to declare and initialize your class variables/fields/attributes in your constructor. You don't
have to in Python, but it's a good idea.
Quote: If I create all class variables in the constructor method why can I create the variable 'turn' in the roll() method?
Because Python lets you. I would argue that it's not good practice but it's allowed by the language.
Quote: Basically I am forced to create list[] in __init__ but all other fields through any function? Very confused...
No, you don't have to initialize
throws in the constructor but you do have to initialize it before calling self.turns.append().
Here are my files:
#class1.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 class1 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()
bash-3.2$ python3.6
Python 3.6.3 (v3.6.3:2c5fed86e0, Oct 3 2017, 00:32:08)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import run
die is - 6
die is - 3
>>>