Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Python __ne__ Magic Method

#1
Python __ne__ Magic Method

To customize the behavior of the non-equality operator x != y, override the __ne__() dunder method in your class definition. Python internally calls x.__ne__(y) to compare two objects using x != y. If the __ne__() method is not defined, Python will use the is not operator per default that checks for two arbitrary objects whether they reside on a different memory address.

Syntax


__ne__(self, other)

To use the not equal to operator on custom objects, define the __ne__() “dunder” magic method that takes two arguments: self and other. You can then use attributes of the custom objects to determine if one is not equal to the other. It should return a Boolean True or False.

Let’s have a look at an example next.

Example


In the following code, you check if a Person is not equal to another Person by using the age attribute as a decision criterion:

class Person: def __init__(self, age): self.age = age def __ne__(self, other): return self.age != other.age alice = Person(18)
bob = Person(19)
carl = Person(18) print(alice != bob)
# True print(alice != carl)
# False

Because Alice is 18 years old and Bob is 19 years old, and 18 != 19 is True, the result of alice != bob is True. But the result of alice != carl evaluates to False as both have the same age.

Background Video





Default Implementation of __ne__


Per default, the __ne__() dunder method is implemented using the is not identity operator. Identity operators are used to check whether two values or variables reside at a different memory location, i.e., refer to a different object in memory.

Because the fallback identity operator is defined for each object, you can also check non-equality for any two objects.

The following example shows that you can compare custom persons using the non-equality operator !=, even without defining the __ne__ method. Internally, Python uses the non-identity operator:

class Person: def __init__(self, age): self.age = age alice = Person(18)
bob = Person(19)
carl = Person(18) print(alice != bob)
# True print(alice != carl)
# True print(alice != alice)
# False

Background Video Identity Operator


To understand the identity operator, feel free to watch the following background video:




Commutativity of Non-Equality !=


The output of x != y and y != x may be different because the former calls x.__ne__(y) and the latter calls y.__ne__(x). If x and y have different definitions of the dunder method __ne__(), the operation becomes non-commutative.

You can see this in the following example:

class Person: def __ne__(self, other): return 42 class Human: def __ne__(self, other): return 0 alice = Person()
bob = Human() print(alice != bob)
# 42 print(bob != alice)
# 0

Where to Go From Here?


Enough theory, let’s get some practice!

To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

Practice projects is how you sharpen your saw in coding!

Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?

Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!



https://www.sickgaming.net/blog/2021/12/...ic-method/
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

[-]
Discord

[-]
Active Threads
News - Video: We Have A Lot Of Feelings ...
Last Post: xSicKxBot
Yesterday 11:53 PM
» Replies: 0
» Views: 11
News - Iconic Rockstar And Actor Meat Lo...
Last Post: xSicKxBot
Yesterday 11:53 PM
» Replies: 0
» Views: 1
(Indie Deal) FREE Last Horizon, Microids...
Last Post: xSicKxBot
Yesterday 08:25 PM
» Replies: 0
» Views: 10
Microsoft - Celebrating 20 years of Trus...
Last Post: xSicKxBot
Yesterday 08:25 PM
» Replies: 0
» Views: 3
Xbox Wire - New EA Play Member Rewards F...
Last Post: xSicKxBot
Yesterday 08:25 PM
» Replies: 0
» Views: 3
News - PS5 Will Outsell Series X|S 2:1 I...
Last Post: xSicKxBot
Yesterday 08:25 PM
» Replies: 0
» Views: 3
[Tut] Pandas get_dummies() – A Simple Gu...
Last Post: xSicKxBot
Yesterday 04:18 PM
» Replies: 0
» Views: 19
Mobile - Genshin Impact’s Tomo – vision,...
Last Post: xSicKxBot
Yesterday 04:18 PM
» Replies: 0
» Views: 5
News - Community Focus: Parris
Last Post: xSicKxBot
Yesterday 04:18 PM
» Replies: 0
» Views: 8
News - Bedrock: BETA 1.18.10.28
Last Post: xSicKxBot
Yesterday 04:18 PM
» Replies: 0
» Views: 6

[-]
Twitter



Discord Server © SickGaming.net 2012-2021