I'm studying Python. I made it as a memo, so there may be some typographical errors.
A class is a blueprint, and an instance is a materialization of that blueprint.
Object-oriented is a way of thinking that captures the behavior of a system as an interaction between objects.
With object orientation, classes and instances were conceived.
class Person(object):
def say_something(self):
print('hello')
# Call a class to create an object
person = Person()
person.say_something()
>>>hello
class Person(object):
#Initialization and initialization image
def __init__(self):
print('First')
def say_something(self):
print('hello')
person = Person()
>>>First
# You can't let yourself hold a value without self
class Person(object):
def __init__(self, name):
self.name = name
print(self.name)
def say_something(self):
print('hello')
person = Person('Mike')
>>>Mike
# Call in a function
class Person(object):
def __init__(self, name):
self.name = name
print(self.name)
def say_something(self):
print('I am {}. hello'.format(self.name))
person = Person('Mike')
person.say_something()
>>>Mike
>>>I am Mike. hello
The first object that is initialized and called is the constructor Last called object
class Person(object):
def __init__(self, name):
self.name = name
print(self.name)
def say_something(self):
print('I am {}. hello'.format(self.name))
def __del__(self):
print('good bye')
person = Person('Mike')
person.say_something()
>>>Mike
>>>I am Mike. hello
>>>good bye
Inherit and write code neatly
class Car(object):
def run(self):
print('run')
# pass means do nothing
class ToyotaCar(Car):
pass
class TeslaCar(Car):
def auto_run(self):
print('auto_run')
car = Car()
car.run()
toyota_car = ToyotaCar()
toyota_car.run()
print('##################')
tesla_car = TeslaCar()
# Since it inherits the Car class, it can be executed.
tesla_car.run()
tesla_car.auto_run()
>>>run
>>>run
>>>##################
>>>run
>>>auto_run
class Car(object):
def __init__(self, model=None):
self.model = model
def run(self):
print('run')
class ToyotaCar(Car):
print('fast')
class TeslaCar(Car):
def __init__(self, model='Model S', enable_auto_run=False):
super().__init__(model)
self.enable_auto_run = enable_auto_run
def run(self):
print('super fast')
def auto_run(self):
print('auto_run')
car = Car()
car.run()
toyota_car = ToyotaCar('Lexus')
toyota_car.run()
print('##################')
tesla_car = TeslaCar('Model S')
tesla_car.run()
tesla_car.auto_run()
>>>fast
>>>run
>>>run
>>>##################
>>>super fast
>>>auto_run
When using properties, when it is okay to rewrite under certain conditions
class Car(object):
def __init__(self, model=None):
self.model = model
def run(self):
print('run')
class ToyotaCar(Car):
print('fast')
class TeslaCar(Car):
def __init__(self, model='Model S', enable_auto_run=False):
super().__init__(model)
self._enable_auto_run = enable_auto_run
# Description that cannot be rewritten
@property
def enable_auto_run(self):
return self._enable_auto_run
def run(self):
print('super fast')
def auto_run(self):
print('auto_run')
teslaCar = TeslaCar('Model S')
print(tesla_Car.enable_auto_run)
>>>False
# k Description when rewriting is acceptable
class Car(object):
def __init__(self, model=None):
self.model = model
def run(self):
print('run')
class ToyotaCar(Car):
print('fast')
class TeslaCar(Car):
def __init__(self, model='Model S', enable_auto_run=False):
super().__init__(model)
self._enable_auto_run = enable_auto_run
@property
def enable_auto_run(self):
return self._enable_auto_run
@enable_auto_run.setter
def enable_auto_run(self, is_enable):
self._enable_auto_run = is_enable
def run(self):
print('super fast')
def auto_run(self):
print('auto_run')
tesla_Car = TeslaCar('Model S')
# Put True first
tesla_Car.enable_auto_run = True
print(TeslaCar.enable_auto_run)
>>>True
・ If you generate a class and enter a value later, it will lead to a bug if you do not check the contents of the class.
class Person(object):
def __init__(self, age=1):
self.age = age
def drive(self):
if self.age >= 18:
print('ok')
else:
raise Exception('No drive')
class Baby(Person):
def __init__(self, age=1):
if age < 18:
super().__init__(age)
else:
raise ValueError
class Adult(Person):
def __init__(self, age=18):
if age >= 18:
super().__init__(age)
else:
raise ValueError
baby = Baby()
adult = Adult()
class Car(object):
def __init__(self, model=None):
self.model = model
def run(self):
print('run')
def ride(self, person):
person.drive()
car = Car()
car.ride(adult)
>>>ok
import abc
class Person(metaclass=abc.ABCMeta):
def __init__(self, age=1):
self.age = age
@abc.abstractclassmethod
def drive(self):
pass
class Baby(Person):
def __init__(self, age=1):
if age < 18:
super().__init__(age)
else:
raise ValueError
def drive(self):
raise Exception('No drive')
class Adult(Person):
def __init__(self, age=18):
if age >= 18:
super().__init__(age)
else:
raise ValueError
def drive(self):
print('ok')
baby = Baby()
adult = Adult()
>>>ok
class Person(object):
def talk(self):
print('talk')
def run(self):
print('parson run')
class Car(object):
def run(self):
print('car run')
# The argument put on the left side executes first
class PersonCarRobot(Car, Person):
def fly(self):
print('fly')
parson_car_robot = PersonCarRobot()
parson_car_robot.talk()
parson_car_robot.run()
parson_car_robot.fly()
>>>talk
>>>car run
>>>fly
Shared by all objects
class Person(object):
kind = 'human'
def __init__(self, name):
self.name = name
def who_are_you(self):
print(self.name, self.kind)
a = Person('A')
a.who_are_you()
b = Person('B')
b.who_are_you()
>>>A human
>>>B human
You can call class methods even if they are not objects
class Person(object):
kind = 'human'
def __init__(self):
self.x = 100
@classmethod
def what_is_your_kind(cls):
return cls.kind
@staticmethod
def about(year):
print('about human {}'.format(year))
a = Person()
print(a.what_is_your_kind())
b = Person()
print(b.what_is_your_kind())
Person.about(1998)
>>>human
>>>human
>>>about human 1998
class Word(object):
def __init__(self, text):
self.text = text
def __str__(self):
return 'Word!!!!!!!!!!!'
def __len__(self):
return len(self.text)
def __add__(self, word):
return self.text.lower() + word.text.lower()
def __eq__(self, word):
return self.text.lower() == word.text.lower()
w = Word('text')
w2 = Word('text')
print(w == w2)
Recommended Posts