PDA

View Full Version : my first python class...


inkedmn
04-03-2002, 07:25 PM
oh yeah, enter the OO...


class myName:
name = ''
age = 0
def __init__(self, newName, newAge):
self.change(newName, newAge)

def printinfo(self):
print self.name, self.age

def change(self, newName, newAge):
self.name = newName
self.age = newAge

if __name__ == '__main__':
somecrap = myName('Brett', 23)
somecrap.printinfo()


w00t! (thanks duck!)

kmj
04-03-2002, 07:32 PM
Why do you call it myName if it contains information on your age, too?

inkedmn
04-03-2002, 07:34 PM
becuase i'm horribly short-sighted and i have serious personal issues.

oh, and i have no idea... :)

kmj
04-03-2002, 09:08 PM
(c: Just bustin' your chops, hombre. (though, of course, having accurate names will make other people who have to maintain your code hate you a little less... ) (^=

inkedmn
04-03-2002, 09:38 PM
revised:


class myNameandAlsoMyAgeJustInCasePeopleCantFigureItOut:
name = ''
age = 0
def __init__(self, newName, newAge):
self.change(newName, newAge)

def printinfo(self):
print self.name, self.age

def change(self, newName, newAge):
self.name = newName
self.age = newAge

if __name__ == '__main__':
somecrap = myNameandAlsoMyAgeJustInCasePeopleCantFigureItOut('Brett', 23)
somecrap.printinfo()



:D

sorry, couldn't resist

kmj
04-04-2002, 10:07 AM
yeah; but what in can't you want to add things to your class, like suddenly you decide that you need you haircolor; I think somethink like "myNameAndAgeAndOtherCharacteristicsthatIMayLaterFindImportantAndPossiblySomeInformationOnMyDogsAndOr OtherPetsTooOhAndMyCar" would be better; just in case.

Strike
04-05-2002, 11:00 AM
Actually, that change() function looks like what could be used for __getattr__

Check here for more info:
http://www.python.org/doc/current/ref/attribute-access.html