On 6/06/20 9:36 AM, zljubi...@gmail.com wrote:
Hi,
if I define class like this one:
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?
The from moduleNM import object/can't use a keyword issue has been
discussed.
When a JSON object is load-ed into a Python program, its native form is
a dict. Thus, "from" will be one of the dict's keys, cf a Python
identifier.
Why have you chosen to go to the trouble of creating a separate
data-attribute (with the from/_from/from_ name)? (I realise the above
class is likely 'cut-down' to suit publication) Does the answer include
terms such as 'understanding', 'control', 'easier to use', 'better
suited to existing code'?
Assuming a good reason (why wouldn't we?), then it is necessary for the
Python object to include (or be able to access) 'serialise' and/or
'deserialise' routines - because Python objects will not do this, in and
of themselves. This is the opportunity to rename the value between its
JSON version and its internal (code) use.
If there are <<many keys..."from">> then they can't all be called "from"
anyway. Plus, a term such as "from", likely fails to convey its full
meaning, eg a student might be enrolled_since, or "zljubisic" has been
paid an higher salary rate_from. As such, 'expanding' its name would
likely improve readability, comprehension, and thus code-quality!
(I'm with the "ick" when it comes to 'artificial' name-mangling)
--
Regards =dn
--
https://mail.python.org/mailman/listinfo/python-list