That Python code has no classes ...

This is the second day article of PythonAdventCalendar2019.

Write a common mistake.

Many methods are defined, but no class is defined.

There was a source with a lot of methods, and the functionality was huge.

Someone said that there was no problem with software that runs Python as a function.

If you don't create a useless instance, you wonder why there isn't one at all.

I "Ah...Why is there no class in this source? "

Opponent "What is a class?"

** I didn't explain it verbally, but I was confused about it. ** **

Python3 class description

With just the methods, you might think that Python doesn't need it because the classes are retrofitted,

"All the functionality of object-oriented programming is in Python classes," he said.

Written in the official documentation.

I would like you to try using the class.

[Object Oriented](https://ja.wikipedia.org/wiki/%E3%82%AA%E3%83%96%E3%82%B8%E3%82%A7%E3%82%AF%E3% 83% 88% E6% 8C% 87% E5% 90% 91)

One of the advantages of object-oriented programming is the use of encapsulated objects that group data and methods together. The second point is that the program can be extended or partially modified by inheritance or overriding. There are two possible merits because you can be aware of object orientation just by creating a class.

If you are not a programmer and continue to copy and paste software that can use Python functions from a book, you will find that a mysterious program file is created. It's not bad, but it's the owner of the program files that has trouble modifying or extending it. As you can see, programming has a low threshold, but it is deep.

Code example

class ClassName:
    def __init__(self):
        name = ''
Ishikawa = ClassName()
Ishikawa.name = 'Ken'
print(Ishikawa.name)

Output result

Ken

All you have to do is write a class like this and add methods.

Python is in demand on the market, and some people don't even know the meaning of the class even if it's used in the field. If you're a beginner and don't know the class, you'll find that it's easier to extend by introducing classes and modifying the code to benefit from object-orientation. At one point in my field, some people using Python didn't know the class.

The end

** Thank you for reading the article for a long time. ** **

import sys

class Hello:
    def __init__(self):
        name = ''
        days = 0
        cost = 0
        hour = 0
        money = 0

# days =Days, cost=Cost, hour=Time, money=Hourly wage
World = Hello()
World.days = 20
World.cost = 4000
World.hour = 4
World.money = 832
print("¥"+"{:,}".format(World.money * World.hour * World.days - World.cost)+"Circle")

¥ 62,560

I wrote it in Python using a class. Payroll program.

Payroll program, RubyAdventCalendar2019 side

Thank you again this year. I look forward to working with you next year.

Recommended Posts

That Python code has no classes ...
python3.9 has new features that no one is paying attention to ...
[Python] Attribute Error:'list' object has no attribute'replace'
python character code
[Python] Algorithm-aware code
Python code that removes contiguous spaces into one
Solution when module'XXX' has no attribute'XXX' in Python
[VS Code] Python language Server no longer starts
Python code acceleration approach
Rewrite Python2 code to Python3 (2to3)
Let's write python code that parses go code and generates go code
infomap python draw code
Before writing Python code
Python classes are slow
About Python3 character code
[Python] tkinter Code that is likely to be reused
Python basic course (13 classes)
[Python] pandas Code that is likely to be reused
I felt that I ported the Python code to C ++ 98.
Python Requests status code
OpenCV basic code (python)
Weird Python error message ——Is that code really executed?
[Python] Countermeasures for "AttributeError:'NoneType' object has no attribute'group'" of googletrans
Python code that keeps tweeting "Bals" as much as you can
Create code that outputs "A and pretending B" in python
The story that Python stopped working with VS Code (Windows 10)
Get country code with python
AttributeError:'NoneType' object has no attribute'loader'
[Python] ModuleNotFoundError: No module named'urlparse'
About python objects and classes
Python code memo for yourself
[Python] Frequently used library code
Debug Python with VS Code
Python classes learned in chemoinformatics
2.x, 3.x character code of python
Paiza Python Primer 8: Understanding Classes
Stop Omxplayer from Python code
Python frequently used code snippets
Generate QR code in Python
[Python] Sample code for Python grammar
Character code learned in Python
Convert python 3.x code to python 2.x
Note that it supports Python 3
Document Python code with Doxygen
Python library "Jusho" that converts postal code ⇔ address all over Japan
Write Python code that applies type information at runtime using pydantic