--All objects contained in Python are objects --Objects contain both data (called variables, attributes) and code (called functions, methods). --An object represents a unique instance (entity, example) of something concrete. --An object can be thought of as a noun, and an object's method can be thought of as a verb. Objects define individual things, and methods define how they interact with others.
--Objects are likened to plastic boxes, but ** classes ** are like molds ** for making such ** boxes.
#Definition of Person class
>>> class Person():
... pass
...
#Objects are created by calling the class name like a function.
>>> someone=Person()
>>> class Person():
... def __init(self):
... pass
...
#__init__()Is a special name given to the method that initializes an individual object when it is created from the class definition. Also, when defining in a class, the first argument must be self.
#The self argument refers to the created object itself.
#Self the newly created object, another argument("Elmer Fudd")As the name of the object__init__()Call the method.
>>> class Person():
... def __init__(self,name):
... self.name=name
...
>>> hunter = Person("Elmer Fudd")
>>> print("The mighty hunter:",hunter.name)
The mighty hunter: Elmer Fudd
--Inside the Person class, the name attribute is called self.name, and from outside the object (hunter) it is called hunter.name.
I could understand the concept of objects and classes, but it was difficult to get used to the grammar ...
"Introduction to Python3 by Bill Lubanovic (published by O'Reilly Japan)"
Recommended Posts