Python Forum
Getting attribute and changing it through method
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Getting attribute and changing it through method
#1
Hi, i have class with few getters
Lets say that one of getters return value which is also object of another class
Something like that:

class Mask(object):
def __init__(self,names): pass
def setBit(self,bit,value=False): pass
def getBit(self,bit): pass

class SNMP_ENGINE(object):
def __init__(self,params):
self._mask=Mask([])
def some_function_that_do_something_with_mask(self):pass
@property
def mask(self):
return self._mask
@mask.setter
def mask(self,value):
self._mask=value
some_function_that_do_something_with_mask()

Now, i need to write such code:

s=SNMP_ENGINE()
my_mask=s.mask
my_mask.setBit(2,True)
s.mask=my_mask

But i can't write such code:

s=SNMP_ENGINE()
s.mask.setBit(2,True) because s.mask returned object of Mask and setBit changed it but s don't know about it

Thanks for your help
Reply
#2
1. Use Code Tags. Make it so much easier to help.
2. Give a running sample.

Seem to be working.
class Mask(object):
    def __init__(self,names):
        self.bit = 8

    def setBit(self,bit,value=False):
        self.bit = bit

    def getBit(self,bit):
        return self.bit

class SNMP_Engine(object):
    def __init__(self,params):
        self._mask=Mask(None)

    def some_function_that_do_something_with_mask(self):pass

    @property
    def mask(self):
        return self._mask

    @mask.setter
    def mask(self,value):
        self._mask=value

snmp = SNMP_Engine(None)
mask = snmp.mask
mask.setBit(2, True)
#snmp.mask = mask # don't need since class is pass by reference
print(mask.bit)
print(snmp.mask.bit)

snmp.mask.setBit(4, True)
print(mask.bit)
print(snmp.mask.bit)

mask.setBit(5, True)
print(mask.bit)
print(snmp.mask.bit)
99 percent of computer problems exists between chair and keyboard.
Reply
#3
Thank you Windspar
Here is working example with tags
class Mask(object):
     def __init__(self,names): pass
     def setBit(self,bit,value=False): pass
     def getBit(self,bit): pass

class SNMP_ENGINE(object):
     def __init__(self,params):
        self._mask=Mask([])
     def some_function_that_do_something_with_mask(self):
         print('Do something')
     @property
     def mask(self):
        return self._mask
     @mask.setter
     def mask(self,value):
         self._mask=value
         self.some_function_that_do_something_with_mask()

print('Version 1')
s=SNMP_ENGINE(None)
my_mask=s.mask
my_mask.setBit(2,True)
s.mask=my_mask
After running we will get such print:

Version 1:
----------- Do something
Version 2:

In version 2 @mask.setter will not execute
Reply
#4
It works fine. You only have Do something print in the mask setter.
print('Version 1')
s = SNMP_ENGINE(None)
my_mask = s.mask
my_mask.setBit(2,True)
print('Version 2')
s.mask = my_mask

Just to let you know
s.mask = my_mask # this is the setter function
This is somewhat pointless because the object is reference.
You are just resigning the self._mask with the same data.
and calling some_function_that_do_something_with_mask.

You probably be better if Mask held a callback to some_function_that_do_something_with_mask.
sample
class Mask(object):
     def __init__(self,names, callback):
         self.names = names
         self.bit = 0
         self.value = False
         self.callback = callback

     def setBit(self,bit,value=False):
         self.bit = bit
         self.value = value
         self.callback()

     def getBit(self):
         return self.bit

class SNMP_ENGINE(object):
     def __init__(self,params):
        self._mask=Mask([], self.some_function_that_do_something_with_mask)

     def some_function_that_do_something_with_mask(self):
         print('Do something')

     @property
     def mask(self):
        return self._mask

     @mask.setter
     def mask(self,value):
         self._mask=value
         self.some_function_that_do_something_with_mask()

print('Version 1')
s = SNMP_ENGINE(None)
my_mask = s.mask
my_mask.setBit(2,True)
print('Version 2')
#s.mask = my_mask
99 percent of computer problems exists between chair and keyboard.
Reply
#5
Thank you Windspar
So there is not builtin mechanism in Python which update container object when child object changed?
Reply
#6
No. That what callbacks are for. How would the object know if child object updated. Without the child telling parent. Look at what I did.
Quote:So there is not builtin mechanism in Python which update container object when child object changed?
Is there a program language that you think does this.
Because I don't know of any.

I thought of one way. The parent would know if the child updated.
If parent was keeping a watchful eye. So the parent has a loop/function that checking for changes.
But this only happens when the parent decide to check if child objects has change.
99 percent of computer problems exists between chair and keyboard.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  class, attribute and method Frankduc 9 2,462 Feb-27-2022, 09:07 PM
Last Post: deanhystad
  how can I changing a base class method voidptr 2 2,260 Nov-10-2019, 10:53 PM
Last Post: voidptr

Forum Jump:

User Panel Messages

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