Bottom Page

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!
Yoriz wrote Jul-09-2019, 08:19 PM:
Please post all code, output and errors (in it's entirety) between their respective tags. I did it for you this time, Here are instructions on how to do it yourself next time.
Quote
#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.
Quote
#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
Quote
#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

Quote
#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
Age Doesn't Matter, Your Passion Does
- Shreya Jain
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  class returns NoneType Object istemihan 0 62 Aug-12-2019, 11:47 AM
Last Post: istemihan
  Object and type class Uchikago 2 128 Jul-28-2019, 10:35 AM
Last Post: DeaD_EyE
  Why does this return False? gunitinug 1 98 Jul-13-2019, 07:11 AM
Last Post: perfringo
  Need help with 'return' in python Leo12143 2 278 Jul-10-2019, 02:35 AM
Last Post: SheeppOSU
  How to save a class object to a file? SheeppOSU 2 153 Jun-22-2019, 11:54 PM
Last Post: metulburr
  saving (in text or binary) an object under a defined class cai0824 3 310 May-12-2019, 08:55 AM
Last Post: snippsat
  Call a varaible from class in the parent class Clement_2000 1 319 May-09-2019, 11:14 PM
Last Post: michalmonday
  Get variable from class inside another class hcne 3 267 Mar-30-2019, 03:02 PM
Last Post: ichabod801
  sort lists of lists with multiple criteria: similar values need to be treated equal stillsen 2 247 Mar-20-2019, 08:01 PM
Last Post: stillsen
  How to continue after "return" Helmi 4 400 Mar-17-2019, 09:26 PM
Last Post: Helmi

Forum Jump:


Users browsing this thread: 1 Guest(s)