What's this setter error ("X" is not defined...) when using a property?

54 Views Asked by At
@property
def GetURLLabel(self):
    return self._URLLabel

# Setter methods
@URLLabel.setter
def SetURLLabel(self, value):
    if isinstance(value, str):
        self._URLLabel = value
    else:
        raise ValueError("Must be a string")

I get the error:

"URLLabel" is not defined
PylancereportUndefinedVariable
(function) URLLabel: Any

Here is the object code:

class SSLSite:
    def __init__(self, URLLabel, x, x, ...):
        self._URLLabel = URLLabel

I am working to set the value of the properties, and URLLabel is one of them.

3

There are 3 best solutions below

0
Giba On

As you can see here you need to declare @property before of setter. Try do this:

class SSLSite:
    def __init__(self, URLLabel, x, y):
        self._URLLabel = URLLabel

    @property
    def URLLabel(self):
        return self._URLLabel

    @URLLabel.setter
    def URLLabel(self, value):
        if isinstance(value, str):
            self._URLLabel = value
        else:
            raise ValueError("Must be a string")
0
mkrieger1 On

You have 3 different names:

  • GetURLLabel as the name of the getter method
  • SetURLLabel as the name of the setter method
  • URLLabel in the decorator of the setter method

The names must all be the same. If you want the name of the property to be URLLabel, you must define two methods called URLLabel, the first one decorated with @property and the second one decorated with @URLLabel.setter.

0
zalaroy On

I believe my issue was that I was using the method name "GetURLLabel". Once I dropped the Get from method name it worked great. Thank you, guys! I apreciate your responses, friends!