A class is a blueprint or template that defines the properties (attributes) and behaviors (methods) that all objects of that type can have.
Here's how to create a simple Person class in the Python interpreter:
class Person:
def __init__(self, name, age):
self.name = name
self.age = ageThe __init__ method is Python's constructor. It helps initialize a new Person object with specific values:
class Person:
def __init__(self, name, age):
self.name = name
self.age = ageLet's create some Person objects using our class:
>>> person1 = Person("Alice", 25)
>>> person1
<__main__.Person object at 0x7f8b8c0b7040>
>>> person2 = Person("Bob", 30)
>>> person2
<__main__.Person object at 0x7f8b8c0b7070>You can access the properties of a Person object using dot notation:
>>> person1.name
'Alice'
>>> person1.age
25Let's add some methods to our Person class:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def say_hello(self):
print(f"Hello, my name is {self.name}")
def have_birthday(self):
self.age = self.age + 1
print(f"Happy Birthday! {self.name} is now {self.age}")Now we can use these methods with our Person objects:
>>> person1.say_hello()
Hello, my name is Alice
>>> person1.have_birthday()
Happy Birthday! Alice is now 26Try creating your own Person class with:
- Create the basic class structure
- Add properties (name and age)
- Create a constructor (
__init__method) - Create two Person objects
- Access their properties
- Add and use methods
- Forgetting
selfas the first parameter in methods - Not using proper indentation (Python uses indentation instead of braces)
- Forgetting to use
self.when accessing instance variables - Not initializing variables in the
__init__method
- Always test your class by creating objects and using their methods
- Use meaningful names for variables and methods
- Keep your code organized and properly indented
- Remember that each object is independent and has its own set of values
- Use
selfto refer to the current instance
You can use these Python interpreter commands to verify your work:
dir(person1)- shows all attributes and methods of the person1 objectvars(person1)- shows all instance variableshelp(Person)- shows documentation for the Person class
Here's a more complete Person class:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def print_details(self):
print(f"Name: {self.name}")
print(f"Age: {self.age}")
def is_adult(self):
return self.age >= 18
def have_birthday(self):
self.age = self.age + 1
print(f"Happy Birthday! {self.name} is now {self.age}")Try using this extended example:
>>> person = Person("John", 17)
>>> person.print_details()
Name: John
Age: 17
>>> print(person.is_adult())
False
>>> person.have_birthday()
Happy Birthday! John is now 18
>>> print(person.is_adult())
TrueThis guide should give you a solid foundation for understanding how to create and use classes in Python. Remember to practice by creating your own variations of the Person class and experimenting with different properties and methods.
Maybe add a method that sets and gets a person's "chirality" (left-handed or right-handed) or "handedness" (ambidextrous, left-handed, right-handed). What type of variable would be best for this? What would be the default value?
You have to add a variable declaration for the handedness in the __init__ method.
And add a getter and setter for the handedness.
Be sure to add a method to print out the handedness.
class Person:
def __init__(self, name, age, handedness="right-handed"):
self.name = name
self.age = age
self.handedness = handedness
def get_handedness(self):
return self.handedness
def set_handedness(self, handedness):
valid_options = ["left-handed", "right-handed", "ambidextrous"]
if handedness in valid_options:
self.handedness = handedness
else:
print(f"Invalid handedness. Choose from: {valid_options}")
def print_handedness(self):
print(f"{self.name} is {self.handedness}")
def print_details(self):
print(f"Name: {self.name}")
print(f"Age: {self.age}")
print(f"Handedness: {self.handedness}")Save it in a file called person.py and also do the trinity of github commands:
git add .
git commit -m "Add Person class work"
git push- No explicit type declarations: Python figures out types automatically
selfinstead of implicitthis: Always useselfas first parameter- Indentation matters: Python uses indentation instead of braces
__init__instead of constructor: Python's special method for initialization- Snake_case naming: Python uses snake_case for method names
Classes and Objects are an essential part of object-oriented programming in Python.
Be sure to do commit of all this work to your GitHub repository.
touch finished.txt
git add .
git commit -m "Finished"
git push