When comparing to None, always use is not. Return self.make = other.make and self.model = other.model What's happening here is that the Monitor object implemented the _eq_ method. However, when we compare them, we want them to compare equal. However, they neither have the same data nor are the same object in memory. > b.warranty_expiration = datetime.date(2005, 8, 22) Rationale: Two objects have different data, but are considered the same object if some key data is the same.Įxample: Most types of model data > import datetime Note: _eq_ on unicode is definitely implemented more efficiently than this. What's happening here is that the unicode object has implemented the _eq_ method. They have the exact same content, but they are not the same object in memory. Here, I have two unicode strings, a and b. Note: I use unicode strings here because Python is smart enough to reuse regular strings without creating new ones in memory. (They are not the same object in memory.)Įxample: Strings > greeting = "It's a beautiful day in the neighbourhood." Rationale: Two objects have the exact same data, but are not identical. This lets you define a _eq_ method on a so that you can use your own comparison logic. When you write if a = b:, you are essentially saying if a._eq_(b). With object equality, you can define your own rules for how two objects compare. Object equality: When you compare two objects, you usually don't care that it refers to the exact same object in memory. We say that cancel, close, and dismiss all refer to identical objects that is, they refer to one single object. You only created one Button object, and all three variables refer to this one object. In this case, cancel, close, and dismiss all refer to the same object in memory. You can then also assign it to another variable. Object identity: When you create an object, you can assign it to a variable. If you just want your question answered, scroll down to "Answering your question".
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |