From a book that programmers can learn (Python): Class declaration (public / private, etc.)

[This book](https://www.amazon.co.jp/%E3%83%97%E3%83%AD%E3%82%B0%E3%83%A9%E3%83%9E%E3 % 81% AE% E8% 80% 83% E3% 81% 88% E6% 96% B9% E3% 81% 8C% E3% 81% 8A% E3% 82% 82% E3% 81% 97% E3% 82 % 8D% E3% 81% 84% E3% 81% BB% E3% 81% A9% E8% BA% AB% E3% 81% AB% E3% 81% A4% E3% 81% 8F% E6% 9C% AC -% E5% 95% 8F% E9% A1% 8C% E8% A7% A3% E6% B1% BA% E8% 83% BD% E5% 8A% 9B% E3% 82% 92% E9% 8D% 9B% E3% 81% 88% E3% 82% 88% E3% 81% 86% EF% BC% 81-% E3% 82% A2% E3% 82% B9% E3% 82% AD% E3% 83% BC% E6 % 9B% B8% E7% B1% 8D-% EF% BC% B6% EF% BC% 8E% EF% BC% A1% EF% BD% 8E% EF% BD% 94% EF% BD% 8F% EF% BD% 8E-% EF% BC% B3% EF% BD% 90% EF% BD% 92% EF% BD% 81% EF% BD% 95% EF% BD% 8C-ebook / dp / B00MB2STXK? Ie = UTF8 & btkr = 1 & ref_ = dp-kindle-redirect) How can I write in python? I'm studying the idea of programming.

Chapter 5 Problem Solving by Class Although it was this content, I had never thought about python class design, so when I searched variously, it was posted in [here Qiita](http://qiita.com/0xfffffff7/items/6ef16e79fe9886acb3f2) There was a very easy-to-understand article, so I used it as a reference.

First, there was a "class declaration" as an issue, so I rewrote it to python.

Class declaration

test.cpp


class sample {
public:
    sample();
    sample(int num);
    int doesSomething(double param);

private:
    int intData;
};

When thinking about this, I had never thought about it separately for "public" and "private", so I refer to the reference site and change it below.

test501.py


#!/usr/bin/env python
#coding:utf-8

class Sample(object):

#public:
    #C++Sample()
    def sample1(self):
        print("sample")

    #C++Sample(int num)
    def sample2(self,r):
        return int(r)

    #C++Int does Something(double param)
    def doesSomething(self,param):
        return float(param)

##Corrected below
    #private:
    __intData = 5  ###The call is_name of the class__Variable name
    

>>>(Terminal execution)
>>> from test501 import Sample
>>> w = Sample()
>>> w.sample1()
sample
>>> w.sample2(1)
1
>>> w.doesSomething(1.5)
1.5
>>> w._Sample__intData
5


At first, in the c ++ code, sample () and sample (1) were written with the same method name, so if you prepared the same one in python, an error would occur and you can move it by changing the name, so be careful about how to use the method name. I noticed the point to do (´Д`) Themes such as encapsulation will continue, but I would like to continue studying what happens to python class design.

Recommended Posts

From a book that programmers can learn (Python): Class declaration (public / private, etc.)
From a book that programmers can learn ... (Python): Pointer
From a book that programmers can learn ... (Python): About sorting
From a book that programmers can learn (Python): Decoding messages
From a book that programmers can learn (Python): Find the mode
From a book that programmers can learn ... (Python): Review of arrays
From a book that programmers can learn (Python): Statistical processing-deviation value
From a book that programmers can learn (Python): Conditional search (maximum value)
From a book that programmers can learn ... Collect small problem parts
From a book that programmers can learn: Verification of rune checksum (fixed length)
From a book that programmers can learn ... Verification of rune checksum (variable length) Part 2
From a book that programmers can learn: Converting characters that represent numbers to integer types
From a book that makes the programmer's way of thinking interesting (Python)
8 services that even beginners can learn Python (from beginners to advanced users)
[python] I made a class that can write a file tree quickly
Generate a class from a string in Python
A class for PYTHON that can be operated without being aware of LDAP
I tried to create a class that can easily serialize Json in Python
I made a Docker image that can call FBX SDK Python from Node.js
[Python / Tkinter] A class that creates a scrollable Frame
"Python Kit" that calls a Python script from Swift
Python: Create a class that supports unpacked assignment
A mechanism to call a Ruby method from Python that can be done in 200 lines
I tried running python etc. from a bat file
Programmer's way of thinking is from XX book (Python)
"A book that understands Flask from scratch" Reading memo
Programmer's way of thinking is from XX book (Python)
I tried to make a generator that generates a C # container class from CSV with Python
A memo that reads data from dashDB with Python & Spark
A Python program in "A book that gently teaches difficult programming"
python Condition extraction from a list that I often forget
How to generate a public key from an SSH private key
[MQTT / Python] Implemented a class that does MQTT Pub / Sub in Python
A Python program that aggregates time usage from icalendar data