Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Python Classes
#1
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?
Quote
#2
I would suggest to check 2 of our class tutorials:
Class basics
Class: inheritance
micseydel likes this post
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Using classes? Can I just use classes to structure code? muteboy 5 1,768 Nov-01-2017, 04:20 PM
Last Post: metulburr
  use of classes in python Lux 2 1,280 Aug-19-2017, 12:29 PM
Last Post: hbknjr
  python classes prsdll6 14 3,242 Aug-17-2017, 07:26 PM
Last Post: nilamo

Forum Jump:


Users browsing this thread: 1 Guest(s)