0
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'
petezurich
  • 9,280
  • 9
  • 43
  • 57
Sean
  • 1
  • 2
    `lights` shouldn't even be defined yet. This apparently *re*defines `lights`, which previously was a function that returns `None` when called. – chepner Jan 15 '20 at 17:05
  • showing more code could be helpfull. – Gocht Jan 15 '20 at 17:07
  • What are you trying to do? This code has a few problems, probably makes sense to understand your main aim in order to better help ... – w08r Jan 15 '20 at 17:08
  • Does this answer your question? [How can I define a class in Python?](https://stackoverflow.com/questions/1495666/how-can-i-define-a-class-in-python) – G. Anderson Jan 15 '20 at 17:09

1 Answers1

1

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.

gabriel.hayes
  • 2,267
  • 12
  • 15
  • 1
    `class lights (object):` is entirely correct, though inheriting from `object` is neither useful nor necessary in Python 3. The problem is that the `l1` object creation as a class global attempts to instantiate the class before it has been defined. – tripleee Jan 15 '20 at 17:13
  • @tripleee Thank you, I'm not a big Python buff, will correct answer – gabriel.hayes Jan 15 '20 at 17:17
  • ok so Im still a bit perplexed. So I need to define l1 first and then go ahead with this? @tripleee – Sean Jan 15 '20 at 17:50
  • @Sean you were trying to create a `lights` instance inside of the declaration of `lights`, which would cause an infinite loop (if it worked, but it doesn't, because `lights` doesn't exist until after the declaration is over) Python is a whitespace based language, the way you had the code indented, you were trying to create `l1` and `l2` inside the declaration of `lights` – gabriel.hayes Jan 15 '20 at 17:53
  • Basically, all I have done is move your code creating `l1` and `l2` outside of the declaration of `lights` so that `lights` exists before they are created, and I added a constructor so that `lights` have `color` and `quantity` members. – gabriel.hayes Jan 15 '20 at 17:54
  • ah ok that helped a bit. still somewhat perplexed but this helped. guess I need to keep practicing and understanding the order – Sean Jan 15 '20 at 18:03
  • It would perhaps be more natural and idiomatic if the `__init__` method took `color` and `quantity` keyword arguments - up to you and your applications' requirements whether these can be optional. – tripleee Jan 15 '20 at 18:10
  • one more question @tripleee how do I make a space print('There are ' + l1.color + str(l1.quantity)) since the output looks like this There are blue50 – Sean Jan 15 '20 at 18:14
  • Uh, you add a space where you need one. Maybe look at `format()` and/or f-strings too. – tripleee Jan 15 '20 at 18:19
  • nvm. I figured it out. with + ' ' + – Sean Jan 15 '20 at 18:29