I have a slight issue with behavior in python2.7 while trying to have multiple names for single property.
# test class
class Something():
def __init__( self ):
self._data_pack = 1.0
@property
def data_pack( self ):
return self._data_pack
@data_pack.setter
def data_pack( self, value ):
self._data_pack = value
dataPack = data_pack
x = Something()
print( 'data_pack', x.data_pack ) #>>> 1.0
print( 'dataPack', x.dataPack ) #>>> 1.0
in Python 3
both return correct value of 20
x = Something()
#change value
x.data_pack = 20
# # both read correct value
print( 'data_pack', x.data_pack ) #>>> 20
print( 'dataPack', x.dataPack ) #>>> 20
in Python 2
only data_pack returns new value, dataPack is broken
x = Something()
#change value
x.data_pack = 20
# # only data_pack has correct value, dataPack is broken
print( 'data_pack', x.data_pack ) #>>> 20
print( 'dataPack', x.dataPack ) #>>> 1.0
Can someone please provide some way to make sure that the OLD python behaves the same way? With minimal changes to the source class and without 'magic' (3rd party) modules.
EDIT: adding class derivation helped thanks for your replies
class Something(object):
def __init__( self ):
self._data_pack = 1.0
@property
def data_pack( self ):
return self._data_pack
@data_pack.setter
def data_pack( self, value ):
self._data_pack = value
dataPack = data_pack
CodePudding user response:
Adding class derivation helped solve the issue thanks ( @AKX and @Aron_Atilla_Hegedus)
class Something: >>>> class Something(object):
class Something(object):
def __init__( self ):
self._data_pack = 1.0
@property
def data_pack( self ):
return self._data_pack
@data_pack.setter
def data_pack( self, value ):
self._data_pack = value
dataPack = data_pack