Can I have a class with property named "from"

Ethan Furman ethan at stoneleaf.us
Fri Jun 5 17:50:45 EDT 2020


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".

--
~Ethan~


More information about the Python-list mailing list