Session 3 - Python CLasses
IMPRS Be Smart Summer School
2023-08-07
R scripts are a series of commands that are executed in order
For instane R or Stata (and what we have done so far)
Objects are independent units that have some variables and functions attached to them.
Object: state and behavior bundled together
Name | Awards | Partner |
---|---|---|
Art | 10 | Paul |
Cher | 20 | Sonny |
Paul | 20 | Art |
Sonny | 5 | Cher |
Objects have some variables attribute
(art.award
, art.partner
)
Objects have some methods functions attached to them method
(art.add_award()
)
Object are independent units.
Classes are blueprints in which an object is generated.
An object created wrom a class is called an instance
Usually a class ansers four questions:
attributes
)methods
)__init__
)parent class
)Encapsulation: Self-contained pieces
Abstractation: What is going on underneath is abstracted away
Inheritance: Classes can be organized hierarchially
Polymorphism: Classes can change into different forms from the parents
__init__
: basically a function run automatically when an instance is createdself
: can be read myself. If refers to the instance that is created from it.07_classes.py
When you create a class with inheritance, put the name of the parent class inside the parantheses.
If you need to reach the parent class from a child class, you can call super()
function.
If you define a method with the same name in the child class, the method of the parent will be overwritten.
If this is not the behavior you want, you can call the super function and extend the method over it.
super()
super()