I am a beginning Programmer using Python and am having trouble wrapping my head around WHY I would create a class. Procedural programming I get, but object orientation I would like a little help with.
Say I was creating a GURPS character and I wanted to store his stats.
Code:
#!/usr/bin/env python3
#name
name = raw_input ("What is your characters name?")
#stats
str = raw_input ("What is your characters Strength score?")
int = raw_input ("What is your characters Intelegence score?")
dex = raw_input ("What is your characters Dexterity score?")
heal = raw_input ("What is your characters Health score?")
#print your character
print "Name:",name
print "Strength: ",str
print "Intelegence: ",int
print "Dexterity: ",dex
print "Health: ",heal
Easy peasy! But how, and more importantly WHY would I do this as an object?
Code:
class GurpChar(object):
"""this class is the player character"""
def __init__(self,name =None,str=None,dex=None,heal=None,int=None):
self.name = name
self.str = str
self.dex = dex
self.heal = heal
self.int = int
return self.name
I just cant wrap my mind around it. I've been searching the internet for a dialog about why one would use Objects and Classes and Functions,and cannot seem to find one which is understandable. Is it is just "the norm"?
Thanks for your input,
Bookmarks