Can someone help me understand special methods vs normal methods? - python

What is the difference between using a special method and just defining a normal class method? I was reading this site which lists a lot of them.
For example it gives a class like this.
class Word(str):
'''Class for words, defining comparison based on word length.'''
def __new__(cls, word):
# Note that we have to use __new__. This is because str is an immutable
# type, so we have to initialize it early (at creation)
if ' ' in word:
print "Value contains spaces. Truncating to first space."
word = word[:word.index(' ')] # Word is now all chars before first space
return str.__new__(cls, word)
def __gt__(self, other):
return len(self) > len(other)
def __lt__(self, other):
return len(self) < len(other)
def __ge__(self, other):
return len(self) >= len(other)
def __le__(self, other):
return len(self) <= len(other)
For each of those special methods why can't I just make a normal method instead, what are they doing different? I think I just need a fundamental explanation that I can't find, thanks.

It is a pythonic way to do this:
word1 = Word('first')
word2 = Word('second')
if word1 > word2:
pass
instead of direct usage of comparator method
NotMagicWord(str):
def is_greater(self, other)
return len(self) > len(other)
word1 = NotMagicWord('first')
word2 = NotMagicWord('second')
if word1.is_greater(word2):
pass
And the same with all other magic method. You define __len__ method to tell python its length using built-in len function, for example. All magic method will be called implicitly while standard operations like binary operators, object calling, comparision and a lot of other. A Guide to Python's Magic Methods is really good, read it and see what behavior you can give to your objects. It similar to operator overloading in C++, if you are familiar with it.

A method like __gt__ is called when you use comparison operators in your code. Writing something like
value1 > value2
Is the equivalent of writing
value1.__gt__(value2)

"Magic methods" are used by Python to implement a lot of its underlying structure.
For example, let's say I have a simple class to represent an (x, y) coordinate pair:
class Point(object):
def __init__(self, x, y):
self.x = x
self.y = y
So, __init__ would be an example of one of these "magic methods" -- it allows me to automatically initialize the class by simply doing Point(3, 2). I could write this without using magic methods by creating my own "init" function, but then I would need to make an explicit method call to initialize my class:
class Point(object):
def init(self, x, y):
self.x = x
self.y = y
return self
p = Point().init(x, y)
Let's take another example -- if I wanted to compare two point variables, I could do:
class Point(object):
def __init__(self, x, y):
self.x = x
self.y = y
def __eq__(self, other):
return self.x == other.x and self.y == other.y
This lets me compare two points by doing p1 == p2. In contrast, if I made this a normal eq method, I would have to be more explicit by doing p1.eq(p2).
Basically, magic methods are Python's way of implementing a lot of its syntactic sugar in a way that allows it to be easily customizable by programmers.
For example, I could construct a class that pretends to be a function by implementing __call__:
class Foobar(object):
def __init__(self, a):
self.a = a
def __call__(self, b):
return a + b
f = Foobar(3)
print f(4) # returns 7
Without the magic method, I would have to manually do f.call(4), which means I can no longer pretend the object is a function.

Special methods are handled specially by the rest of the Python language. For example, if you try to compare two Word instances with <, the __lt__ method of Word will be called to determine the result.

The magic methods are called when you use <, ==, > to compare the objects. functools has a helper called total_ordering that will fill in the missing comparison methods if you just define __eq__ and __gt__.
Because str already has all the comparison operations defined, it's necessary to add them as a mixin if you want to take advantage of total_ordering
from functools import total_ordering
#total_ordering
class OrderByLen(object):
def __eq__(self, other):
return len(self) == len(other)
def __gt__(self, other):
return len(self) > len(other)
class Word(OrderByLen, str):
'''Class for words, defining comparison based on word length.'''
def __new__(cls, word):
# Note that we have to use __new__. This is because str is an immutable
# type, so we have to initialize it early (at creation)
if ' ' in word:
print "Value contains spaces. Truncating to first space."
word = word[:word.index(' ')] # Word is now all chars before first space
return str.__new__(cls, word)
print Word('cat') < Word('dog') # False
print Word('cat') > Word('dog') # False
print Word('cat') == Word('dog') # True
print Word('cat') <= Word('elephant') # True
print Word('cat') >= Word('elephant') # False

Related

Python: Accessing dict with hashable object fails

I am using a hashable object as a key to a dictionary. The objects are hashable and I can store key-value-pairs in the dict, but when I create a copy of the same object (that gives me the same hash), I get a KeyError.
Here is some small example code:
class Object:
def __init__(self, x): self.x = x
def __hash__(self): return hash(self.x)
o1 = Object(1.)
o2 = Object(1.)
hash(o1) == hash(o2) # This is True
data = {}
data[o1] = 2.
data[o2] # Desired: This should output 2.
In my scenario above, how can I achieve that data[o2] also returns 2.?
You need to implement both __hash__ and __eq__:
class Object:
def __init__(self, x): self.x = x
def __hash__(self): return hash(self.x)
def __eq__(self, other): return self.x == other.x if isinstance(other, self.__class__) else NotImplemented
Per Python documentation:
if a class does not define an __eq__() method it should not define a __hash__() operation either
After finding the hash, Python's dictionary compares the keys using __eq__ and realize they're different, that's why you're not getting the correct output.
You can use the __eq__ magic method to implement a equality check on your object.
def __eq__(self, other):
if (isinstance(other, C)):
return self.x == self.x
You can learn more about magic methods from this link.
So as stated before your object need to implement __ eq__ trait (equality ==), If you want to understand why:
Sometimes hash of different object are the same, this is called collision.
Dictionary manages that by testing if the objects are equals. If they are not dictionary has to manage the collision. How they do that Is implementation details and can vary a lot. A dummy implementation would be list of tuple key value.
Under the hood, a dummy implementation may look like that :
dico[key] = [(object1, value), (object2, value)]

Special Values of a Class

Right now I am creating a class which represents a closed interval. Its core functionality is to provide an intersect method.
class Interval:
def __init__(self, a, b):
# check a <= b otherwise swap
self.a = a
self.b = b
def intersect(self, other):
a = self.a if self.a > other.a else other.a
b = self.b if self.b < other.b else other.b
if b < a:
# return some value representing an empty interval, providing the intersect method
return Intervall(a,b)
It should be possible to represent special Values like all points [-oo,oo] or the empty set {}. Which still serve the intersect method. My current approach is to create a new class, but this seems kinda tedious.
class EmptyInterval:
def intersect(self, other):
return self
Assuming those special values' intersect methods take precedence I'd prepend on the Intervall class' method:
class Intervall:
...
def intersect(self,other):
if not isinstance(self, other):
other.intersect(self)
...
To clarify - the following should be legal:
a = Intervall(1,2)
b = Intervall(3,4)
c = a.intersect(b) # resulting in an empty interval
c.intersect(a) # resulting again in an empty interval
Is there some elegant / more pythonic / less nauseating ugly way to implement such a behavior?
First I thought of inheritance, but that seems quite unfitting because of the precedence those special values should have; i.e. I do not know how to implement it via inheritance.
Define a couple special functions in your class Interval:
#staticmethod
def everything():
return Interval(-math.inf, math.inf)
#staticmethod
def nothing():
return Interval(math.nan, math.nan)
You may find it more natural to write nothing() like this:
return Interval(0, 0)
or this:
return Interval(math.inf, math.inf)
It rather depends on your other code, and what you think is the most natural way to represent the empty interval. Note that any less or greater comparison with NAN will return false, so this may have some impact on which way you decide to represent the empty interval (for example is nothing().intersect(nothing()) supposed to be true or false?).
Maybe this could be another solution:
Instead of passing a,b separately, I could pass a tuple (a,b). Further I could declare a couple of singletons as class variables. During instantiation I'd pass that singleton and would only have to check whether that value is one of the singletons and act accordingly.
class Interval:
EMPTY = object()
EVERYTHING = object()
def __init__(self, bounds):
self.bound = bounds
def intersect(self, other):
if self.bounds == self.EMPTY or other.bounds == self.EMPTY:
return Interval(self.EMPTY)
...
if b < a:
return Interval(self.EMPTY)
return Interval((a,b))
I guess this maybe less error prone than John's answer, because of the general behavior math.inf and / or math.nan impose. Also it would allow to strictly forbid those values to be passed, as Interval(math.nan, 1) would be nonsensical.
But it may be more effort to implement in a more complex setting.

Efficiently implementing operator overloads in Python [duplicate]

I am in need of a reflected magic method "greater than" and there does not appear to be one. Here is the situation. I have a class which keeps track of units. It is call Property. I have the magic method setup to handle comparisons, but it does not work when I put the Property on the right side. Here is an example:
class Property():
def __init__(self, input, units):
self.value = input
self.units = units
def __gt__(self, other):
if isinstance(other, Property):
return self.value.__gt__(other.value)
else:
return self.value.__gt__(other)
def __float__(self):
return float(self.value)
if __name__=='__main__':
x = Property(1.,'kg')
y = Property(0.,'kg')
print y > x
print float(y) > x
print y > float(x)
So if you run this you will see the output is: False, True, False because the middle example is executing float > Property which uses the built in > not the > I have defined using magic methods. I need a magic method that will be used when the Property is on the right hand side. Is that not a thing? If not, how can I write this so that any combination of values and my own class can be compared. I would like to not have any rules for comparisons. IE, I don't want to just never be able to compare a float to a property.
You can use a functools.total_ordering decorator to create the missing comparison methods for you:
import functools
#functools.total_ordering
class Property():
...
Then you get False, False, False. Do make sure to read its documentation, though.
__lt__ is __gt__'s counterpart; you'll need to implement __lt__. While you're at it, you should probably implement __le__ and __ge__.
Since you've declared the __float__() method, you could always write it as:
print float(y) > float(x)

reflected greater than magic methods

I am in need of a reflected magic method "greater than" and there does not appear to be one. Here is the situation. I have a class which keeps track of units. It is call Property. I have the magic method setup to handle comparisons, but it does not work when I put the Property on the right side. Here is an example:
class Property():
def __init__(self, input, units):
self.value = input
self.units = units
def __gt__(self, other):
if isinstance(other, Property):
return self.value.__gt__(other.value)
else:
return self.value.__gt__(other)
def __float__(self):
return float(self.value)
if __name__=='__main__':
x = Property(1.,'kg')
y = Property(0.,'kg')
print y > x
print float(y) > x
print y > float(x)
So if you run this you will see the output is: False, True, False because the middle example is executing float > Property which uses the built in > not the > I have defined using magic methods. I need a magic method that will be used when the Property is on the right hand side. Is that not a thing? If not, how can I write this so that any combination of values and my own class can be compared. I would like to not have any rules for comparisons. IE, I don't want to just never be able to compare a float to a property.
You can use a functools.total_ordering decorator to create the missing comparison methods for you:
import functools
#functools.total_ordering
class Property():
...
Then you get False, False, False. Do make sure to read its documentation, though.
__lt__ is __gt__'s counterpart; you'll need to implement __lt__. While you're at it, you should probably implement __le__ and __ge__.
Since you've declared the __float__() method, you could always write it as:
print float(y) > float(x)

decorator __lt__ and others - why use these and how do they work?

I stumpled into a problem of understanding the decorators. Compared to the code below, what is the use of the __lt__(self, number) method compared to the less_than(self, number) method?
Is there any difference between the two methods and how they handle the arguments?
class Sort:
def __init__(self, number):
self.number = number
def __lt__(self, number):
return self.number < number
def less_than(self, number):
return self.number < number
The two functions, __lt__() and less_than() do the same thing. However, there is a big difference:
When you use the "<" operator, the function __lt__ is called internally by python. So, you can do this
x = Sort(5)
y = Sort(10)
print(x < y)
The comparison x < y calls x.__lt__(y), and so it returns true. In this way you can change the behavior of built in operators for the specific class you have created. See "operator overloading" and "python magic methods" for more details
As Martijn Pieters said this has nothing to do with decorators.
s1 = Sort(1)
s2 = Sort(2)
if s1 < s2:
print 'Yes'
if s1.less_than(2):
print 'Yes'
See the difference?
In the first if you compare to objects of type Sort.
In the second if you invoke a method from instance s1 of type Sort.

Categories

Resources