TommyD543
TommyD543

Reputation: 1

Python Class Objects/Attributes

I am learning python with an online course and have been doing fine, I have stumbled across and issue. Normally when having an issue i just google to find the answer or look up guides but the problem here is I'm not even sure what I'm looking for!

I currently have the below code. I have a task to make each workout intensity have a specific value, such as Low = 3, Medium = 6 and High = 12. Then I can find the calories burned via duration * the values dependant on the intensity and duration passed into the class.

What I don't know is how do I assign a value to a class method? I tried lists and Dictionarys and both are throwing errors. I tried writing and If statement to try if self.intensity(getattr) = "Low": Then x = 3, etc.

I really am not sure where to even start to find an answer hence asking you guys and girls.

The code currently is (I am aware pieces are missing also I'm currently only focusing on assigning values to the Intensity

class ExerciseSession:
def __init__(self, exercise, intensity, duration):
    self.exercise = exercise
    self.intensity = intensity
    self.duration = duration

def get_exercise(self):
    return self.exercise

def get_intensity(self):
    return self.intensity

def get_duration(self):
    return self.duration + " minutes"

def set_exercise(self, excersice):
    self.set_exercise = exercise

def set_intensity(self, intensity):
    self.set_intensity = intensity

def set_duration(self, duration):
    self.set_duration = duration   

new_exercise = ExerciseSession("Running", "Low", 60)
print(new_exercise.get_exercise())
print(new_exercise.get_exercise())
print(new_exercise.get_intensity())
print(new_exercise.get_duration())
new_exercise.set_exercise("Swimming")
new_exercise.set_intensity("High")
new_exercise.set_duration(30)
print(new_exercise.get_exercise())
print(new_exercise.get_intensity())
print(new_exercise.get_duration())
print(new_exercise.get_intensity())
print(new_exercise.get_duration())
new_exercise.set_exercise("Swimming")
new_exercise.set_intensity("High")
new_exercise.set_duration(30)
print(new_exercise.get_exercise())
print(new_exercise.get_intensity())
print(new_exercise.get_duration())

Am i just doing lists/dictionaries wrong within a class or am I missing something incredibly easy here. I understand classes and methods but it seems some things work slightly different when inside a class etc.

Upvotes: 0

Views: 138

Answers (2)

Bluetail
Bluetail

Reputation: 1291

doing the same :-)

def calories_burned(self):
    if self.intensity == "Low":
        return 4 * self.duration
    elif self.intensity == "Medium":
        return 8 * self.duration
    else:
        return 12 * self.duration
    

Upvotes: 0

hemmelig
hemmelig

Reputation: 350

Firstly - are your setters implemented as they are in your question? If so, you appear to be trying to override your methods with a value. There is also a small typo in set_exercise(). Compare the below with your question:

def set_exercise(self, exercise):
    self.exercise = exercise

def set_intensity(self, intensity):
    self.intensity = intensity

def set_duration(self, duration):
    self.duration = duration   

You can then write your test statements outside of the class to figure out what value to use when calculating the number of calories consumed based on the intensity.


Alternatively, Python provides a neat way of encapsulating data without resorting to getter/setter methods through the @property decorator. The property can then be set and retrieved in a simple fashion. This is particularly useful in more complex situations where an attribute is derived from other attributes, meaning you will always access the most up-to-date attributes. This is covered in more detail here: "public" or "private" attribute in Python ? What is the best way?

class ExerciseSession:
    def __init___(self, exercise, intensity, duration):
        self._exercise = exercise
        self._intensity = intensity
        self._duration = duration

    @property
    def exercise(self):
        return self._exercise

    @property
    def intensity(self):
        if self._intensity == "Low":
            out = 3
        elif self._intensity == "Medium":
            out = 6
        elif self._intensity == "High":
            out = 12
        else:
            out = None

        return out

    @property
    def duration(self):
        return self._duration

Note: the "_" before each instance attribute is used to indicate the internal attribute is conventionally private.

These properties can then be accessed as follows (note we do not have to call any methods, e.g. new_exercise.exercise()):

new_exercise = ExerciseSession("Running", "Low", 60)
print(new_exercise.exercise)
print(new_exercise.intensity)
print("{} minutes.".format(new_exercise.duration))

If you need to be able to update the type of exercise/duration/intensity on the object, rather than just creating a new one, you can add setter methods using the @.setter decorator, e.g.:

@exercise.setter
def exercise(self, value):
    self._exercise = value

and these properties can be updated as:

new_exercise.exercise = "Swimming"

Upvotes: 1

Related Questions