Can I have a class with property named "from"
MRAB
python at mrabarnett.plus.com
Fri Jun 5 18:15:36 EDT 2020
On 2020-06-05 22:50, Ethan Furman wrote:
> On 06/05/2020 02:36 PM, zljubisic at gmail.com wrote:
>
>> class abc:
>>
>> def __init__(self):
>> self._from = None
>>
>> @property
>> def from(self):
>> return self._from
>>
>> @from.setter
>> def from(self, value):
>> self._from = value
>>
>>
>> I get the error SyntaxError: invalid syntax because of the property named from.
>> Looks like in python from is a keyword that cannot be used as a property name.
>>
>> I have to create a python object from a complex (nested) json object that has many keys with name "from" and value of date in string format.
>>
>> As I would like to keep property names as they are in the json file... is there a workaround?
>
> There is no workaround that allows a keyword to be used except as a keyword, other than making it a string. When faced with this kind of situation myself I use a synonym, like "since", or a translation, like "desde".
>
The usual workaround is to append an underscore: 'from_'.
More information about the Python-list
mailing list