Reputation: 79
so I have a class,
class Interval:
def __init__(self, min, max):
self.min = min
self.max = max
self.compare_mode = None
def min_max(min, max = None)-> 'Interval Object':
'''Code to return properly initialized Interval Object
'''
...
return Interval(min, max)
this is just the main parts of my class for my questions. Here I have two properly initialized Interval objects
l = Interval.min_max(1.0,5.0)
r = Interval.min_max(4.0,6.0)
now I am trying to just update the class' self.compare_mode attribute by just binding certain strings to it with the following code:
Interval.compare_mode = 'liberal'
now when I print out the value from outside the class using print(Interval.compare_mode)
it looks like the value is bounded but when I try access this string from other methods within my Interval class it still initialed to None. I just want to update the class' compare_mode attribute after the Interval objects are created using Interval.compare_mode = 'some string'
at any point this code is initiated.
Upvotes: 1
Views: 2388
Reputation: 1933
There is a difference between the instance attribute and class' attributes.
When you assign to self.compare_mode
you are referring to the instance attribute, when you assign Interval.compare_mode
you refer to the class attribute.
However when reading, first it tries to give you an instance attribute, then if that fails you get the class attribute.
Try this modification:
class Interval:
compare_mode = None # Class attribute
def __init__(self, min, max):
self.min = min
self.max = max
# NOT setting self.compare_mode here
Upvotes: 2