-1
class Camera(object):

    def __init__(self, win, x=0.0, y=0.0, rot=0.0, zoom=1.0):
        self.win = win
        self.x = x
        self.y = y
        self.rot = rot
        self.zoom = zoom
cam = Camera(Window,1,1,1,1)

vs

class Camera(object):

    def __init__(self, win, x, y, rot, zoom):
        self.win = win
        self.x = x
        self.y = y
        self.rot = rot
        self.zoom = zoom
cam = Camera(Window,1,1,1,1)

So does the first block of code just make the class static where it can only be made and not adjusted with parameters? If so, what's the usefulness of this?

vaultah
  • 44,105
  • 12
  • 114
  • 143

2 Answers2

2

The first block of code just sets default values for those variables. When the class is initialised if they are passed to the class, then they will be set. Otherwise the class variables will contain the default values.

In the second block of code, by not setting a default value you are making those parameters required for the class.

See this question for a better explanation: Why do we use __init__ in python classes?

Community
  • 1
  • 1
ScottSmudger
  • 351
  • 2
  • 8
1

The difference between the first one and second one is: the first one has default values while the other one dosen't. For example with the first one cam = Camera(Window) wouldn't give you an error (x=0.0, y=0.0, rot=0.0 and zoom=1.0 have a default assigned value).

But the second code block would give you an error if you were to create new instance of the class like this cam = Camera(Windows). Wich is ultimately the difference between your first code block and your second one.

Cheers!