in javascript:
d={one: false, two: true}
d.one
d.two
d.three
I want to be able to differentiate between d.one
and d.three
. By default they both evaluate to false, but in my case they should not be treated the same.
Python hasattr() function is an inbuilt utility function, which is used to check if an object has the given named attribute and return true if present, else false.
The hasattr() function returns True if the specified object has the specified attribute, otherwise False .
Python hasattr() The hasattr() method returns true if an object has the given named attribute and false if it does not.
You can do
"one" in d // or "two", etc
or
d.hasOwnProperty("one")
You probably want hasOwnProperty as the in
operator will also return true if the property is on the an object in the prototype chain. eg.
"toString" in d // -> true
d.hasOwnProperty("toString") // -> false
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With