Python Forum
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?
Reply
#2
I would suggest to check 2 of our class tutorials:
Class basics
Class: inheritance
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Classes rob101 4 467 Feb-05-2024, 06:51 PM
Last Post: rob101
  Understanding Python classes PythonNewbee 3 1,150 Nov-10-2022, 11:07 PM
Last Post: deanhystad
Sad Python classes PythonNewbee 4 995 Nov-09-2022, 01:19 PM
Last Post: deanhystad
  Inheritance vs Instantiation for Python classes mr_byte31 7 2,789 Oct-14-2021, 12:58 PM
Last Post: mr_byte31
  Understanding Python super() for classes OmegaRed94 1 1,792 Jun-09-2021, 09:02 AM
Last Post: buran
  Python classes Python_User 15 4,753 Aug-04-2020, 06:57 PM
Last Post: Python_User
  Using classes? Can I just use classes to structure code? muteboy 5 4,978 Nov-01-2017, 04:20 PM
Last Post: metulburr
  use of classes in python Lux 2 3,482 Aug-19-2017, 12:29 PM
Last Post: hbknjr
  python classes prsdll6 14 7,250 Aug-17-2017, 07:26 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

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