class Person: def __init__(self,name): self.name = name a = Person('Lisa') b = Person('Lisa') print 'är de lika utan __eq__ ',a == b b = a print 'är de samma objekt nu ',a == b class EqPerson: def __init__(self,name): self.name = name def __eq__(self,other): return self.name == other.name a = EqPerson('Lisa') b = EqPerson('Lisa') print 'är de lika med __eq__',a == b print 'är de samma objekt ',a is b class EqPersonFullname: def __init__(self,name,lastName): self.name = name self.lastName = lastName def __eq__(self,other): return self.name == other.name a = EqPerson('Lisa') b = EqPersonFullname('Lisa','Johansson') print 'är Lisa och Lisa Johansson lika med __eq__',a == b class StrictEqPersonFullname: def __init__(self,name,lastName): self.name = name self.lastName = lastName def __eq__(self,other): return self.name == other.name and self.lastName == other.lastName a = StrictEqPersonFullname('Lisa','Johansson') b = EqPersonFullname('Lisa','Svensson') print 'är nu Lisa och Lisa Johansson lika med __eq__',a == b print 'är nu Lisa och Lisa Johansson lika med __eq__',b == a
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question