How in python to check whether the empty attribute?

Is it possible in python3 when creating an object to test on the fly, if the attribute is empty, and if not, add it?
await db[collection_name].insert_one(
 {"ok": ok
 "lib": lib,
 "step": step,
 "date": date,
 "msg": msg,
 err is not None and "err": err, # mod not mandatory
 "server": server})


This check — err is not None and doesn't work.
I would like some concise decision like how in js to avoid a large of number of IFS ever existed.
April 7th 20 at 15:30
1 answer
April 7th 20 at 15:32
Solution
Ternary expression: <some_value> if <condition> else <another_value>
In the case of the ternary expression attribute is still created in the database with a null value
await db[collection_name].insert_one(
{"err": err if err else None})
- Larue.Boy commented on April 7th 20 at 15:35
@Eldon53logic:
If error is not empty, return it.
If it is empty, then return None (null). - barry_Thomps commented on April 7th 20 at 15:38
@breanne73, so I did. The problem is that the database is written to err: is null. I want to see if err is None, in dB not recorded in err.
I had to write a separate function which checks the attributes of the object, and if they are empty, it adds this attribute to the object. But I thought that python has a more elegant solution like JS:
{err && "err": err}
something - Larue.Boy commented on April 7th 20 at 15:41
@Eldon53why, just add the if and all. All will be clearer, now You understand what this code does. Through the year and You do not understand.
if err is None:
 ... # write error
else:
 ... # do not write error
- barry_Thomps commented on April 7th 20 at 15:44
@Eldon53,
await db[collection_name].insert_one({"err": err if err else None})

The problem is that the database is written to err: is null. I want to see if err is None, in dB not recorded in err.

await db[collection_name].insert_one({"err": err} if err else None)
- Kailey_Beat commented on April 7th 20 at 15:47

Find more questions by tags Python