Skip to content

Instantly share code, notes, and snippets.

@jhgaylor
Created June 10, 2012 01:39
Show Gist options
  • Select an option

  • Save jhgaylor/2903474 to your computer and use it in GitHub Desktop.

Select an option

Save jhgaylor/2903474 to your computer and use it in GitHub Desktop.
A sample of python class code
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