Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
An example of a class example using Python3.
#! /usr/local/bin/python3
class Dog:
#Python's constructor; setting properties similar to JS
def __init__(self, name, breed, age):
self.name = name
self.breed = breed
self.age = age
#Using self to update the name property on the newly created object
def set_name(self, name):
self.name = name
def set_breed(self, breed):
self.breed = breed
def set_age(self, name):
self.name = name
def get_name(self):
return self.name
def get_breed(self):
return self.breed
def get_age(self):
return self.age
def __str__(self):
return ("%s %s %d" % (self.name, self.breed, self.age))
# Create new dog object
myDog = Dog("Isabelle", "Golden Retriever", 2)
print(myDog) #Isabelle Golden Retriever 2
#Update my dog's name to Molly
myDog.set_name("Molly")
print(myDog) #Molly Golden Retriever 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment