Effective Python Learning Memorandum Day 13 [13/100]

Introduction

The other day I learned about 100 Days Of Code, which was popular on Twitter for a while. The purpose of this article is to keep a record and output how much I, as a beginner, can grow through 100 days of study. I think there are many mistakes and difficult to read. I would appreciate it if you could point out!

Teaching materials to be learned this time

--Chapter 8 structure --Page 216 of this chapter

Today's progress

--Progress: Pages 79-86 --Chapter 3: Classes and Inheritance ――I will write down what I often forget or didn't know about what I learned today.

Public attributes are preferred over private attributes

There are public and private visibility of attributes in Python classes. It is treated as private by adding two underscores. ** Only those declared inside the class block can be accessed privately. ** ** For example, subclasses cannot access the parent class's private.

class MyObject(object):
    def __init__(self):
        #public
        self.public_field = 2
        #private
        self.__private_field = 4

    def get_private_field(self):
        return self.__private_field

The public can be accessed from anywhere with the dot operator.

obj = MyObject()
print(obj.public_field)
# 2

Private throws an exception when trying to access it with the. Operator.

print(obj.__private_field)

Output result

AttributeError: 'MyObject' object has no attribute '__private_field'

Let's check the attributes of the object using __dict__.

print(obj.__dict__)
# {'public_field': 2, '_MyObject__private_field': 4}

Public has the same attribute name, but private is _MyObject__private_field instead of __private_field. In Python, the behavior of private attributes is done by simple conversion of attribute names. Therefore, you can easily access your private life by doing the following.

print(obj._MyObject__private_field)
# 4

However, even with this method, the parent's private class is not accessible. In the first place, Python's private is not excluded from subclasses because it can be accessed from the outside by force and it becomes inaccessible when it becomes multiple inheritance. Internal APIs and attributes should be taken into account and protected fields should be described in the documentation. It seems that private use should only be considered when avoiding name conflicts with subclasses.

Custom container type inherits collections.abc

Many methods are required to successfully implement a custom container type. Therefore, it is quite difficult to define your own container type. The module that exists to eliminate them is collections.abc. This module defines an abstract base class that provides all the typical container-type methods. The module will point out that you have created this abstract base class and forgot to implement the required methods.

from collections.abc import Sequence

class BadType(Sequence):
    pass

foo = BadType()
# TypeError: Can't instantiate abstract class BadType with abstract methods __getitem__, __len__

Recommended Posts

Effective Python Learning Memorandum Day 15 [15/100]
Effective Python Learning Memorandum Day 6 [6/100]
Effective Python Learning Memorandum Day 9 [9/100]
Effective Python Learning Memorandum Day 8 [8/100]
Effective Python Learning Memorandum Day 14 [14/100]
Effective Python Learning Memorandum Day 1 [1/100]
Effective Python Learning Memorandum Day 13 [13/100]
Effective Python Learning Memorandum Day 3 [3/100]
Effective Python Learning Memorandum Day 5 [5/100]
Effective Python Learning Memorandum Day 4 [4/100]
Effective Python Learning Memorandum Day 7 [7/100]
Effective Python Learning Memorandum Day 2 [2/100]
Python memorandum
Python Memorandum 2
Python memorandum
python learning
python memorandum
python memorandum
Python day 1
Python memorandum
python memorandum
Python memorandum
Python basics memorandum
[Python] Learning Note 1
Python learning notes
Python pathlib memorandum
python learning output
Deep Learning Memorandum
Python learning site
Python Deep Learning
Python learning (supplement)
Deep learning × Python
Python memorandum [links]
python learning notes
Python study day 1
Python memorandum numbering variables
Python class (Python learning memo ⑦)
Learning Python with ChemTHEATER 03
"Object-oriented" learning with python
Learning Python with ChemTHEATER 05-1
Python: Deep Learning Practices
Python ~ Grammar speed learning ~
python memorandum (sequential update)
Python: Unsupervised Learning: Basics
Learning record 4 (8th day)
Learning record 9 (13th day)
[1day1lang AdventCalender] day4 Python
Learning record 3 (7th day)
Learning record 5 (9th day)
Learning record 6 (10th day)
Python memorandum (personal bookmark)
Programming learning record day 2
Learning record 1 (4th day)
Learning record 7 (11th day)
Learning Python with ChemTHEATER 02
Python basic memorandum part 2
Learning Python with ChemTHEATER 01
Python: Deep Learning Tuning
Learning record 2 (6th day)
Python + Unity Reinforcement Learning (Learning)
Python: Supervised Learning (Regression)