Created
June 10, 2012 01:39
-
-
Save jhgaylor/2903474 to your computer and use it in GitHub Desktop.
A sample of python class code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class MyClass: | |
| def __init__(self, name): #this is your constructor | |
| self.active = False #this is an atrribute of the object... because we are setting it without a parameter we will call this one a 'default' value | |
| self.name = name #we set this attribute from a parameter | |
| def rename(self, newname): | |
| self.name = newname | |
| return True | |
| def __str__(self): #this is the function that will be called any time you try to print an instance of the class. | |
| return "my name is "+self.name | |
| ########################### | |
| #now lets use the class | |
| a = MyClass("Jake") | |
| print a #prints my name is jake | |
| a.rename("Matt") | |
| print a #print my name is Matt | |
| a.name = "George" | |
| print a.name #prints George | |
| a.rename("Jake") | |
| print a.name #prints Jake |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment