Question

Today I am wondering what are the advantages of a class over a callable function. I know it seems like a weird question, but I am coding a space-based game, and I would like to make something that shows lifesupport(oxygen, nitrogen) Basically, I have this function:

oxygen = 100
nitrogen = 100
air = [oxygen, nitrogen]
print("You can use check(air) to see the quantities of n2 and o2 available.")
def check(variable):
    if variable == air:
    print("Your oxygen level is {0}% and your nitrogen level is {1}%.".format(oxygen, nitrogen))

So whenever the player types check(air), they can see the amount of nitrogen and oxygen in their ship. Using this I can also allow them to see other functions by expanding the check() function. I'm wondering if it's better to do this for a game rather than to do this:

class Lifesupport(object):
    def __init__(self, oxygen, nitrogen):
        self.oxygen = oxygen
        self.nitrogen = nitrogen

air = Lifesupport(40, 60)
#This part is for checking functionality
print("Your air has {0}% oxygen and {1}% nitrogen".format(air.oxygen, air.nitrogen))

Personally, I prefer the function, though I don't really know which is better to use for this purpose. I know you can type air.oxygen to see just oxygen levels, and I could probably use a check() function to solely print a class "bundle" like 'air'.."

Basically... What are the real advantages of using a class over a function like the code I showed? Is it better to use a class or a function, or both, for my purposes?

Was it helpful?

Solution

For printing the oxygen and nitrogen, you would do:

class Lifesupport(object):
    def __init__(self, oxygen, nitrogen):
        self.oxygen = oxygen
        self.nitrogen = nitrogen
    def __str__(self):
        return "Your air has {0}% oxygen and {1}% nitrogen".format(self.oxygen, self.nitrogen)

Then later, whenever you want to show the Lifesupport levels, you simply do:

air = Lifesupport(40, 60)
print(air)

The __str__ method overrides the default __str__ method of a class and so when you do print(air), it will print the custom text.

As for class vs. method, it is recommended that you use classes, especially when you know that are going to be expanding your program, since you can create multiple instances of a class that will all have attributes that can be modified independent of each other. Below is an example:

Example

class A:
    def __init__(self,num):
        self.val = num

a = A(4)
b = A(5)

>>> print(a.val)
4
>>> a.val = 6
>>> print(a.val)
6
>>> print(b.val)
5

OTHER TIPS

Class is an Instance factory. You can create multiple instance (a.k.a copy) of class customize & extend it to your need (by Inheritance & overloading methods).

If you ever want to re-use or customize already written function, then using it inside the class is way to go. I suggest you go through the class coding basic if you have not already done that to make up your mind. a simple example could be

class Person:
    def __init__(self, name, job=None, pay=0):
        self.name = name
        self.job = job
        self.pay = pay

    def giveRaise(self, percent):
        self.pay = int(self.pay * (1 + percent))

class Manager(Person):
     def __init__(self, name, pay):            # Redefine constructor
            Person.__init__(self, name, 'mgr', pay) # Run original with 'mgr'
     def giveRaise(self, percent, bonus=.10):
            Person.giveRaise(self, percent + bonus)

I'm customizing the original class 'init' method by inheriting original class & adding new information in Manager Class

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top