일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
- Else
- line width
- continue
- PROJECT
- __init__
- iterates
- multiple lines
- AS
- error
- SQL
- For loops
- matplotlib.pyplot
- Default X points
- polynomial regression
- Python
- matplotlib
- train/test
- MySQL
- data distribution
- PANDAS
- Text Analytics
- break
- Text mining
- start exercise
- machine learning
- self parameter
- Github
- line color
- pie charts
- variables
- Today
- Total
Data Science Explorer
[Basic Grammar] Python Inheritance 본문
- Python Inheritance
Inheritance allows us to define a class that inherits all the methods and properties from another class.
Parent class (base class) is the class being inherited.
Child class(derived class) is the class that inherits from another class.
Example
Create a class named Christmas with month, date properties.
class Christmas:
def__init__(self, month, date)"
self.month = month
self.date = date
def printname (self)"
print (self.month, self.date)
X = Christmas("December", "25")
X.printname()
- Add the __init__ Function
It is called automatically every time the class is being used to create a new object.
Example
Add the __init__ () function to the Parent class;
class Parent(Person):
def __init__ (self, fname, lname):
** When you add the __init__() function, the child class will no longer inherit the parent's __init__() function. **
- super() function
It makes the child class inherit all the methods and properties from its parents.
Example
class Student(Person):
def __init__(self, fname, lname):
super().__init__(fname, lname)
- Add properties
class Parents(Person):
def __init__(self, fname, age):
super().__init__(fname, age)
self.job = engineer
In this code, you just added the property below the super statement. Engineer is a variable and passed into the Parents class when creating parents objects.
'Python' 카테고리의 다른 글
[Basic Grammar] Datetime (0) | 2023.11.23 |
---|---|
[Basic Grammar] Modules (1) | 2023.11.22 |
[Basic Grammar] Python Classes/Objects (1) | 2023.11.19 |
Matplotlib Pie Charts (0) | 2023.11.11 |
Matplotlib Histograms (0) | 2023.11.10 |