Python Forum
Return a value when I equal an object from a class
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Return a value when I equal an object from a class
#1
Hello,

I would like to do something like:


class Hi():
 def read(self):
   return 23


hi = Hi()
val1 = Hi.read()
val2 = Hi
I would like to associate the function read() or whatever returns when I do an equal from the object Hi. In the example val1 and val2 would have the same value.

Is that posible in Python?

Thanks!
Reply
#2
Give your class the __eq__ method.

https://docs.python.org/3/reference/data...ect.__eq__ Wrote:object.__lt__(self, other)
object.__le__(self, other)
object.__eq__(self, other)
object.__ne__(self, other)
object.__gt__(self, other)
object.__ge__(self, other)
  • These are the so-called “rich comparison” methods. The correspondence between operator symbols and method names is as follows: x<y calls x.__lt__(y), x<=y calls x.__le__(y), x==y calls x.__eq__(y), x!=y calls x.__ne__(y), x>y calls x.__gt__(y), and x>=y calls x.__ge__(y).

    A rich comparison method may return the singleton NotImplemented if it does not implement the operation for a given pair of arguments. By convention, False and True are returned for a successful comparison. However, these methods can return any value, so if the comparison operator is used in a Boolean context (e.g., in the condition of an if statement), Python will call bool() on the value to determine if the result is true or false.

    By default, __ne__() delegates to __eq__() and inverts the result unless it is NotImplemented. There are no other implied relationships among the comparison operators, for example, the truth of (x<y or x==y) does not imply x<=y. To automatically generate ordering operations from a single root operation, see functools.total_ordering().

    See the paragraph on __hash__() for some important notes on creating hashable objects which support custom comparison operations and are usable as dictionary keys.

    There are no swapped-argument versions of these methods (to be used when the left argument does not support the operation but the right argument does); rather, __lt__() and __gt__() are each other’s reflection, __le__() and __ge__() are each other’s reflection, and __eq__() and __ne__() are their own reflection. If the operands are of different types, and right operand’s type is a direct or indirect subclass of the left operand’s type, the reflected method of the right operand has priority, otherwise the left operand’s method has priority. Virtual subclassing is not considered.
Reply
#3
Hello,

I don't want to do a comparison, I just want to assign a value within the object without using the .method and read the attribute in the same way. Something like:

class Reg():
  register = 0
  def write(val):
    self.register = val
  def read():
    return self.register

 def __assign__(self, value):
        self.register = value
   
  reg = Reg()
  reg = 5 # Now register is 5, this is equivalent to:
  reg.write(5)

  val = reg.read()
  val = reg # <- This is that I want to archieve,
When I write val = reg I want to read the register value within the reg object. I mean to be equivalent to reg.read(), is it possible to overwrite the behaviour of the "=" in such a way?

Thanks
Reply
#4
(Jul-09-2019, 08:47 PM)ihouses Wrote: is it possible to overwrite the behaviour of the "=" in such a way?

No. You could only do it with item or attribute assignment, which is what you are trying to avoid in the first place.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
You could do it this way
class Reg():
    register = 0
    def write(self, val):
        self.register = val

r = Reg()
r.write(3)
val = r.register
print(val)
Output:
3
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  This result object does not return rows. It has been closed automatically dawid294 5 595 Jan-10-2024, 10:55 PM
Last Post: deanhystad
  error in class: TypeError: 'str' object is not callable akbarza 2 418 Dec-30-2023, 04:35 PM
Last Post: deanhystad
  how to return a reference to an object? Skaperen 8 1,072 Jun-07-2023, 05:30 PM
Last Post: Skaperen
  is there equal syntax to "dir /s /b" kucingkembar 2 940 Aug-16-2022, 08:26 AM
Last Post: kucingkembar
  Can a variable equal 2 things? Extra 4 1,435 Jan-18-2022, 09:21 PM
Last Post: Extra
Exclamation win32com: How to pass a reference object into a COM server class Alfalfa 3 4,768 Jul-26-2021, 06:25 PM
Last Post: Alfalfa
  AttributeError class object has no attribute list object scttfnch 5 3,300 Feb-24-2021, 10:03 PM
Last Post: scttfnch
  Python Class and Object Parameters JoeDainton123 5 2,805 Sep-02-2020, 10:43 AM
Last Post: JoeDainton123
  isinstance() always return true for object type check Yoki91 2 2,481 Jul-22-2020, 06:52 PM
Last Post: Yoki91
  Return boolean from recursive class method medatib531 6 3,393 Jul-13-2020, 04:27 AM
Last Post: medatib531

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020