-1

I am getting an error when I try to use a constant inside a constructor in a class. I have searched for a resolution to the issue all morning and I can make the program function by using if/else statements in the getter but the instructions are to use the constants.

class Fan:

    SLOW = 1
    MEDIUM = 2
    FAST = 3

    def __init__(self, speed=SLOW, radius=5, color='blue', on=False):
        self.__speed = speed
        self.__on = on
        self.__radius = radius
        self.__color = color

    def getSpeed(self):
        return self.__speed

    def getRadius(self):
        return self.__radius

    def getColor(self):
        return self.__color

    def getOn(self):
        return self.__on

    def setSpeed(self, speed):
        self.__speed = speed

    def setRadius(self, radius):
        self.__radius = radius

    def setColor(self, color):
        self.__color = color

    def setOn(self, on):
        self.__on = on

fan1 = Fan(FAST, 10, 'yellow', True)
fan2 = Fan(MEDIUM, 5, 'blue', False)

print(fan1.getSpeed())
print(fan1.getRadius())
print(fan1.getColor())
print(fan1.getOn())
print()
print(fan2.getSpeed())
print(fan2.getRadius())
print(fan2.getColor())
print(fan2.getOn())

When I run this code I get the following error "NameError: name 'FAST' is not defined"

Any help and explanation would be appreciated.

vaultah
  • 44,105
  • 12
  • 114
  • 143
Daniel Miller
  • 57
  • 1
  • 1
  • 10
  • These constants are class attributes, not globals, so you have to access them that way everywhere except within the class definition body. – abarnert Mar 29 '18 at 17:56
  • Note that you don't need `Fan.SLOW` when *defining* `__init__` because at that point, `SLOW` is just a "local" variable at the same "scope" as the function definition. (Quotes used because class definitions are a bit quirky.) – chepner Mar 29 '18 at 17:58
  • As an aside, this looks like code written by a Java programmer that hasn't learned Python yet. None of those getters and setters would be used in idiomatic Python code; the attributes would just be used directly. – chepner Mar 29 '18 at 18:01
  • If you wanted more complex logic to wrap the simple attribute access, the right thing to do would be to define them as properties rather than simple instance attributes. – chepner Mar 29 '18 at 18:03
  • @chepner thank you for the information, the instruction requires use of getters and setters since I am still learning the fundamentals. At least that's my theory since I have came across loads of information to write these programs differently (and more efficiently). – Daniel Miller Mar 29 '18 at 18:04
  • Ah, so you are being taught by someone who can't (or won't) distinguish between idiomatic Python and abstract object-oriented programming. My condolences. – chepner Mar 29 '18 at 18:07

2 Answers2

3

FAST does not exist in your main program. It only exists in your class

you need to refer to FAST within the class

use Fan.FAST instead.

You'll probably also get an error when instantiating a Fan object with the default parameters unless you change SLOW to Fan.SLOW

Mohammad Athar
  • 1,953
  • 1
  • 15
  • 31
  • 1
    The last sentence is false, as I explain in a comment to the question. (The fact that the script even *tries* to execute anything beyond the class definition demonstrates this.) – chepner Mar 29 '18 at 18:00
3

You need:

fan1 = Fan(Fan.FAST, 10, 'yellow', True)
fan2 = Fan(Fan.MEDIUM, 5, 'blue', False)

You need to add Fan. in front of FAST and MEDIUM because these constants are class variables, not global variables.

hausdork
  • 791
  • 7
  • 9