Skip to content
UPEEC

Classes and Objects

A class is a blueprint; objects are instances with their own data.

Easy
Comments 0
class Resistor:
    def __init__(self, ohms, tolerance=0.05):
        self.ohms = ohms
        self.tolerance = tolerance

    def current(self, volts):
        return volts / self.ohms

r = Resistor(220)
r.current(5)      # 0.0227

__init__ initialises each new object; self refers to the object itself. Attributes starting with _ are a convention for "internal — don't touch from outside".

Spotted a mistake or want to add detail? Suggest an improvement — Content Contributors' edits are reviewed by editors before they go live.

Discussion (0)

Sign in or join free to ask questions and discuss this topic.