44 lines
1.9 KiB
Python
44 lines
1.9 KiB
Python
|
"""
|
||
|
Their is going to be a relatively big jump in difficulty here, but dont be scared!
|
||
|
All you need to know to understand classes are variables and functions, as covered in the previous file.
|
||
|
|
||
|
Key Concepts:
|
||
|
**class** is a blueprint for creating objects (specific instances of that class),
|
||
|
a class represents a collection of attributes and methods to represent something (e.g a Person)
|
||
|
|
||
|
**object** is an instance of a class with its own unique data.
|
||
|
|
||
|
**attributes** are values tied to a object
|
||
|
|
||
|
**methods** are the exact same as functions, but are tried to a class.
|
||
|
|
||
|
**Magic/dunder methods** are methods predefined by python that can be used for various things.
|
||
|
|
||
|
**__init__** is a type of magic method, it is known as a constructor and initializes the objects attributes when its created
|
||
|
"""
|
||
|
|
||
|
class Person:
|
||
|
"""A class Representing a Human/Person"""
|
||
|
|
||
|
# This is the constructor, whenever a new person is created it runs and can be used to assign the new person their attributes!
|
||
|
def __init__(self, name, age, eye_color):
|
||
|
self.name = name # An attribute
|
||
|
self.age = age
|
||
|
self.eye_color = eye_color
|
||
|
|
||
|
# This is a method, note "self" must be passed as a paramater/arguement for it to access the attributes of... itself.
|
||
|
def canApplyForLicence(self):
|
||
|
if self.age >= 17:
|
||
|
return f"{self.name} can apply for their driving permit!"
|
||
|
else:
|
||
|
return f"{self.name} is too young ({self.age}) and can not apply for a driving permit."
|
||
|
|
||
|
|
||
|
pedro = Person("Pedro", 19, "Brown") # Creates a new person with the following values
|
||
|
|
||
|
print(pedro.canApplyForLicence()) # Runs the canApplyForLicence method, and prints the return value
|
||
|
|
||
|
Iain = Person("Iain", 16, "Blue") # Creates a Another new person with the following values (iain armitage if you wanted to know.)
|
||
|
|
||
|
print(Iain.canApplyForLicence()) # Runs the canApplyForLicence method, and prints the return value
|