Python Forum

Full Version: Python Classes
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
from random import Random # library for randoms
class Die(object): # define the class
    sides = [1,2,3,4,5,6]
    def __init__(self): # METHOD
        self.sideShowing = Die.sides[0] # PROPERTY
    def roll(self): # METHOD
        newSide = Random().choice(Die.sides)
        self.sideShowing = newSide # PROPERTY
1.Why is there an object in parentheses at the class definition (line 1) ? what is this object and why is it passed as an argument-is it a special type of argument or just the name of an argument?

2.What is the self in parentheses at lines 4 and 7?

3.What is the _init_ method?
I would suggest to check 2 of our class tutorials:
Class basics
Class: inheritance