C / C ++ programmer challenges Python (class)

Here has suppressed the basics (probably w), so I would like to work on the class as the next step.

Basic

--The declaration is "class Hoge:" --Inheritance is "class Hoge (Base1, Base2):" --The method is the same as the function "def func (self, ...):" --Anyway, the first argument of the method is "self" --The constructor is "def \ _ \ _ init \ _ \ _ (self, ...):" --Class variables can also be defined (in Java style) --Other than that, if you set it in "\ _ \ _ init \ _ \ _ ()", it will be the expected operation. --You can add more and more variables to the object (it feels strange to not declare it) --If you don't set a value for a variable, it's the same as if the variable doesn't exist

Tips

--Private functions (variables) are "\ _func ()", with an underscore in front and no underscore in the back. --Static method is defined by " @ static method " --You don't need "self" in the first argument

Other

It seems that there are more functions, but even if you don't remember it for the time being, it seems quite so. I will see it when I need it.

Method name meaning
__init__ constructor
__del__ Destructor
__new__ Instance allocator
__repr__ Returns a string that represents the information of the object
__str__ Converts an object to a string and returns it
__bytes__ Convert object to bytes type and return
__format__ Returns the object as a formatted string
__hash__ Returns the hash value of the object
__bool__ Called during Boolean operation
__lt__ 「<Supports calculation
__le__ 「<=Supports calculation
__eq__ 「==Supports calculation
__ne__ 「!=Supports calculation
__gt__ 「>Supports calculation
__ge__ 「>=Supports calculation
__getattr__ Called when reading an attribute value that is not set in the object (except when it is registered)
__getattribute__ Called (always) when reading an attribute value that is not set on an object
__setattr__ Called when setting the attribute value of an object
__delattr__ Called when deleting an object's attribute value
__dir__ Returns a sequence of a list of members
__index__ Returns an integer value
__add__ 「+Corresponds to the operator (left term)
__sub__ 「-Corresponds to the operator (left term)
__mul__ 「*Corresponds to the operator (left term)
__truediv__ 「/Corresponds to the operator (left term)
__floordiv__ 「//Corresponds to the operator (left term)
__mod__ 「%Corresponds to the operator (left term)
__divmod__ 「divmod()Corresponds to the operator (left term)
__pow__ 「**Corresponds to the operator (left term)
__lshift__ 「<<Corresponds to the operator (left term)
__rshift__ 「>>Corresponds to the operator (left term)
__and__ 「&Corresponds to the operator (left term)
__xor__ 「^Corresponds to the operator (left term)
__or__ Corresponds to the "
__radd__ 「+Corresponds to the operator (right term)
__rsub__ 「-Corresponds to the operator (right term)
__rmul__ 「*Corresponds to the operator (right term)
__rtruediv__ 「/Corresponds to the operator (right term)
__rfloordiv__ 「//Corresponds to the operator (right term)
__rmod__ 「%Corresponds to the operator (right term)
__rdivmod__ 「divmod()Corresponds to the operator (right term)
__rpow__ 「**Corresponds to the operator (right term)
__rlshift__ 「<<Corresponds to the operator (right term)
__rrshift__ 「>>Corresponds to the operator (right term)
__rand__ 「&Corresponds to the operator (right term)
__rxor__ 「^Corresponds to the operator (right term)
__ror__ Corresponds to the "
__iadd__ 「+=Supports cumulative assignment statements
__isub__ 「-=Supports cumulative assignment statements
__imul__ 「*=Supports cumulative assignment statements
__itruediv__ 「/=Supports cumulative assignment statements
__ifloordiv__ 「//=Supports cumulative assignment statements
__imod__ 「%=Supports cumulative assignment statements
__ipow__ 「**=Supports cumulative assignment statements
__ilshift__ 「<<=Supports cumulative assignment statements
__irshift__ 「>>=Supports cumulative assignment statements
__iand__ 「&=Supports cumulative assignment statements
__ixor__ 「^=Supports cumulative assignment statements
__ior__ 「|=Supports cumulative assignment statements
__neg__ 「-Supports unary operators
__pos__ 「+Supports unary operators
__abs__ 「abs()Supports unary operators
__invert__ 「^Supports unary operators
__complex__ complex()Corresponds to
__int__ int()Corresponds to
__float__ float()Corresponds to
__round__ round()Corresponds to
__instancecheck__ Whether the class is a subclass, an instance of a subclass, or an instance of a virtual subclass
__subclasscheck__ Whether the class is a subclass or a virtual subclass
__call__ 「()Corresponds to the function call operator
__len__ len()Corresponds to
__getitem__ object[key]Corresponding to acquisition in
__setitem__ object[key]Corresponds to the setting of
__delitem__ del object[key]Corresponds to
__iter__ Get an iterator
__reversed__ reversed()Corresponds to
__contains__ Supports acquisition by "item in object"
__get__ Get attribute value with descriptor
__set__ Set attribute value in descriptor
__delete__ Delete attribute value in descriptor
__enter__ Called with with statement in context manager
__exit__ Called at the end of the execution context in the context manager

Recommended Posts

C / C ++ programmer challenges Python (class)
C / C ++ programmer challenges Python (first step)
[Python] class, instance
"Kanrika" python class
About python, class
python C ++ notes
python, openFrameworks (c ++)
Python class, instance
#Python basics (class)
python syslog wrapper class
Python class (Python learning memo ⑦)
case class in python
Python C / C ++ Extension Pattern-Pointer
[Python] Class inheritance (super)
Python self-made class sort
[python] class basic methods
[Python] Class inheritance, override
Next Python in C
python subprocess wrapper class
C API in Python 3
ABC147 C --HonestOrUnkind2 [Python]
YOLO Python wrapper class
Class notation in Python
Python exception class list
Python points from the perspective of a C programmer
Extend python in C ++ (Boost.NumPy)
Python: Class and instance variables
ABC163 C problem with python3
Python, Java, C ++ speed comparison
PyTorch C ++ VS Python (2019 Edition)
ABC memorandum [ABC163 C --managementr] (Python)
Python class member scope summary
Binary search in Python / C ++
Python class variables and instance variables
Multi-stage selection (C # / Python) (old)
I tried Python C extension
Python started by C programmers
ABC188 C problem with python3
ABC187 C problem with python
How to write a Python class
ABC memorandum [ABC159 C --Maximum Volume] (Python)
Solve ABC163 A ~ C with Python
Call C from Python with DragonFFI
perl objects and python class part 2.
Create Awaitable with Python / C API
Multi-stage selection (Go / C # / Ruby / Python)
[Python of Hikari-] Chapter 09-03 Class (inheritance)
Landmines hidden in Python class variables
Realize interface class in C language
Python class definitions and instance handling
ABC166 in Python A ~ C problem
"The easiest Python introductory class" modified
Call popcount from Ruby / Python / C #
Read PNG chunks in Python (class)
Introduction to Protobuf-c (C language ⇔ Python)
Python cheat sheet (for C ++ experienced)
[Python] Road to snake charmer (3) Python class
Solve ABC168 A ~ C with Python
ABC memorandum [ABC161 C --Replacing Integer] (Python)
Solve ABC036 A ~ C in Python
Tips for calling Python from C