Is it possible to partially override a method? What I mean for example I have one method and I want to update it with some modifications, but I don't want to change whole method (I couldn't figure out how to update old method by inheriting it, but not entirely changing it). So is it possible to do something like this:
class A(object):
def test(self, num):
tmp = 3
res = tmp + num
print
return res
a = A()
a.test(5)
returns 8
class B(A):
def test(self, num):
tmp = 5
a = super(B, self).test(num)
return a
b = B()
b.test(5)
returns 10
Is there a way to update only tmp
value, leaving res = tmp + num, so it would use it as it is defined in A
class. Or if I want to update like this, there is only way to rewrite everything in that method(because using super I only get the final value that method returns)?
As suggested I update question with method I try to only partially update it:
def _display_address(self, cr, uid, address, without_company=False, context=None):
'''
The purpose of this function is to build and return an address formatted accordingly to the
standards of the country where it belongs.
:param address: browse record of the res.partner to format
:returns: the address formatted in a display that fit its country habits (or the default ones
if not country is specified)
:rtype: string
'''
# get the information that will be injected into the display format
# get the address format
address_format = address.country_id and address.country_id.address_format or \
"%(street)s\n%(street2)s\n%(city)s %(state_code)s %(zip)s\n%(country_name)s"
args = {
'state_code': address.state_id and address.state_id.code or '',
'state_name': address.state_id and address.state_id.name or '',
'country_code': address.country_id and address.country_id.code or '',
'country_name': address.country_id and address.country_id.name or '',
'company_name': address.parent_id and address.parent_id.name or '',
}
for field in self._address_fields(cr, uid, context=context):
args[field] = getattr(address, field) or ''
if without_company:
args['company_name'] = ''
elif address.parent_id:
address_format = '%(company_name)s\n' + address_format
return address_format % args
So for this method I only need to update/change address_format
and args
, inserting additional values.
Since tmp
is a local variable of A
's test
function, there is no (portable) way to access it. Either make tmp
another parameter of test
, and just pass in the appropriate value (possible with a suitable default if none is passed), or make it a field of A
and override that field's value in B
.
Edit: Seeing the actual code, and learning that you aren't allowed to change it, makes this a lot more difficult. Basically, you would have to copy-paste the superclass's code and just change the parts necessary. The good solutions all require modifying the superclass's method.