Sean
Sean

Reputation: 1

How do can I fix this issue about 'NoneType' error?

class lights(object):
    l1 = lights()
    l1.color = "blue"
    l1.quantity = 50
​
    l2 = lights()
    l2.color = "red"
    l2.quantity = 50    ​

AttributeError                            Traceback (most recent call last)
<ipython-input-11-1ae6b23b2a3d> in <module>
----> 1 class lights(object):
      2     l1 = lights()
      3     l1.color = "blue"
      4     l1.quantity = 50
      5 

in lights() 1 class lights(object): 2 l1 = lights() ----> 3 l1.color = "blue" 4 l1.quantity = 50 5

AttributeError: 'NoneType' object has no attribute 'color'

Upvotes: 0

Views: 85

Answers (1)

gabriel.hayes
gabriel.hayes

Reputation: 2313

It seems you are wanting a constructor to define what properties lights may have:


class lights(object):
  def __init__(self):
    self.color = ""
    self.quantity = 0


l1 = lights()
l1.color = "blue"
l1.quantity = 50
l2 = lights()
l2.color = "red"
l2.quantity = 50

This will run and create two lights instances.

Upvotes: 1

Related Questions