-2

I am going through the book "Problem Solving with Algorithms and Data Structures using Python"

In the chapter for Queues there is a printer simulation with the Printer class.

here is the definition of Printer class:

class Printer():
def __init__(self, ppm):
    self.pagerate = ppm
    self.currentTask = None
    self.timeRemaining = 0

My question is that how are the instance variable not present in parameter but still defined (e.g. currentTask and timeRemaining)?

Is it a practice in Python and is there any other better way to do this?

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69

2 Answers2

0

From the documentation https://docs.python.org/3/tutorial/classes.html#class-objects

The instantiation operation (“calling” a class object) creates an empty object. Many classes like to create objects with instances customized to a specific initial state. Therefore a class may define a special method named __init__(), like this:

def __init__(self):
    self.data = []

Also Instance variables vs. class variables in Python

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
Risadinha
  • 16,058
  • 2
  • 88
  • 91
0

You don't need to pass values for all parameters. By writing self.variable_name we automatically create instance variables. They don't need to be initiated with passed values. You can initiate them with None values.

Protik Nag
  • 511
  • 5
  • 20
  • well none of the documents provided here answer my question.But I guess i can take away from this question that i can create variable w/o passing values.Maybe I did not frame the question right. – rahul agarwal Mar 25 '19 at 19:27