0

I have type a code using classes and without using classes but i cant find why classes are useful.Both give the same output.Please help Here are the codes ;

1)

class Hero:
    def __init__(self,name,health,p,s):
        self.name=name
        self.health=health
        self.p=int(p)
        self.a=int(s)
        food=vars()
        print(self.name,self.health)
        self.eat()
        print(self.health)
    def eat(self):
        print('choose a or b')
        food=input('>')
        if food == 'a':
            self.health -= self.p
        elif food == 'b':
            self.health += self.a
        else:
            print('choose one among a and b')


Hero('Bob',100,10,5)
Hero('Ham',500,60,10)

2)

def eat(health,p,a):
    print('choose a or b')
    food=input('>')
    if food == 'a':
        health -= int(p)
    elif food == 'b':
        health += int(a)
    else:
        print('choose one among a and b')
    return health
def Hero(name,health,p,a):
    print(name,health)
    health = eat(int(health),p,a)
    print(health)
Hero('Bob',100,10,5)
Hero('Ham',500,60,10)

Someone please help.

  • Your question asks why are classes useful. Would you mind explaining why the functional form might be more useful? – jpp Mar 07 '18 at 15:23
  • How would you let bob eat something else in either example? If using classes, they maintain an internal state, think why this could be useful. – jbndlr Mar 07 '18 at 15:25
  • 1
    Possible duplicate of [Python - Classes and OOP Basics](https://stackoverflow.com/questions/10004850/python-classes-and-oop-basics) – Gsk Mar 07 '18 at 15:29
  • The briefest explanation I can think of is that classes manage distinct groups of "global" variables that need to be shared between function calls. – chepner Mar 07 '18 at 15:31

3 Answers3

5

Classes are at the core of Object Oriented Programming (OOP), a programming paradigm (a "way" of writing programs), as opposed to Procedural Programming (PP) - the paradigm you used in the second code snippet.

Fundamentally, they both have the same capabilities. There is not something you can do in OOP but you can do in PP or vice-versa. However, OOP provides some convenient shortcuts that makes code easier to write and work with for us, humans:

  • Encapsulation - you can have objects that contain some data representing their intrinsic properties and methods that operate on those objects, but you can hide all these internals and expose just what is useful for the user to achieve the object's scope (disclaimer - this might be a bit dry, just give it some time and think about it and why it is useful; also, python is not the best language to see this)

  • Abstraction - the user of the object does not need to know the internals of your object. Think about in real world when you drive a car. You need to know how to use the controls (e.g. steering wheel, brake) but you don't necessarily need to know how the engine works - the car abstracts the engine details from the driver. You can do a similar thing with classes

You also have ways of representing relations between objects and "stealing" part of their behavior. For example, you can model the fact that a cat is an animal, and has the basic attributes of an animal (e.g. alive, moves), and some particular attributes that make it a cat.

All of these have proven to be of great aid in writing large pieces of software, thus making OOP one of the most widely used programming paradigm. This doesn't mean that the same functionality cannot be achieved through PP, or that some of the features OOP offers cannot be simulated in PP. It's just more cumbersome.

Paul92
  • 8,827
  • 1
  • 23
  • 37
  • I would also add that this isn't a "one or the other" problem for Python, where you can easily incorporate PP (just functions) in OOP solutions. The idea being that generic functions can be made in a PP-way and specific methods in an OOP-way. – jpp Mar 07 '18 at 15:34
  • Indeed, most OOP languages support PP too. However, there is a debate to be made about Python since everything is an object, you can't be purely procedural. – Paul92 Mar 07 '18 at 15:40
  • Are most programmes written in oop?? – Hemant Kumar Chodipilli Mar 08 '18 at 14:45
  • It's hard to say if most of them are written in OOP, but it is certainly a very common programming paradigm, and it is usually preferred nowdays for bigger projects, unless there is a reason to write procedurally. – Paul92 Mar 08 '18 at 14:59
0

Python is a very versatile language. It allows both functional and Object Oriented programming. Java or Ruby force the programmer into the OO world, while C is not very OO friendly.

That being said, the OO paradigm is to have objects and methods that can acts on those objects, and over the OO paradigm, you have polymorphism that allows specializations for some classes of objects. This paradigm is relevant at the functional level (before you begin any coding) and can be implemented in any language including C and assembly if it is relevant for the question.

If your functional analysis ends into a mere procedural tree, OO is of little interest. For example to implement the Sieve of Eratosthenes, or solve the Eight Queens Puzzle (search on Wikipedia if you do not know them) OO adds little value, and a Java implementation will probably use one single class because nothing more is needed.

On the other hand, if your analysis find objects with many relations between them, then OO will be a natural tool.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
0

Typically a small project starts to get bigger as more functionalities are added. At that point of time, maintaining code becomes tedious.

To encounter this, practices such as inheritance, composition, design patterns were developed. I would recommend to read about https://www.toptal.com/python/python-design-patterns.

It's not only about the output of the code. It's about how you write a code. Software developers are not paid just to write a piece of code, they are paid to write maintainable and robust code.

BTW, I do not understand why you need eat(). You are not using it anywhere.

JR ibkr
  • 869
  • 7
  • 24