Refactoring Learned in Python (Basic)

Extract Function

before.py


import math

def main():
    radius = int(input())
    circumference = math.pi * radius * 2
    print(circumference)

if __name__ == '__main__':
    main()

after.py


import math

def get_circumference(radius):
    return math.pi * radius * 2

def main():
    radius = int(input())
    circumference = get_circumference(radius)
    print(circumference)

if __name__ == '__main__':
    main()

Inline Function

before.py


import math

def get_circumference(radius):
    return math.pi * radius * 2

def print_circumference(circumference):
    print(circumference)

def main():
    radius = int(input())
    circumference = get_circumference(radius)
    print_circumference(circumference)

if __name__ == '__main__':
    main()

after.py


import math

def get_circumference(radius):
    return math.pi * radius * 2

def main():
    radius = int(input())
    circumference = get_circumference(radius)
    print(circumference)

if __name__ == '__main__':
    main()

Extract Variable

before.py


import math

def get_circumference(radius):
    return math.pi * radius * 2

def main():
    print(get_circumference(int(input())))

if __name__ == '__main__':
    main()

after.py


import math

def get_circumference(radius):
    return math.pi * radius * 2

def main():
    radius = int(input())
    circumference = get_circumference(radius)
    print(circumference)

if __name__ == '__main__':
    main()

Inline Variable

before.py


import math

def get_circumference(radius):
    return math.pi * radius * 2

def main():
    radius_str = input()
    radius_int = int(radius_str)
    circumference = get_circumference(radius)
    print(circumference)

if __name__ == '__main__':
    main()

after.py


import math

def get_circumference(radius):
    return math.pi * radius * 2

def main():
    radius = int(input())
    circumference = get_circumference(radius)
    print(circumference)

if __name__ == '__main__':
    main()

Change Function Declaration

before.py


import math

def crcm(radius):
    return math.pi * radius * 2

def main():
    radius = int(input())
    circumference = cr(radius)
    print(circumference)

if __name__ == '__main__':
    main()

after.py


import math

def get_circumference(radius):
    return math.pi * radius * 2

def main():
    radius = int(input())
    circumference = get_circumference(radius)
    print(circumference)

if __name__ == '__main__':
    main()

Encapsulate Variable

before.py


import math

def get_diameter(radius):
    return radius * 2

def get_circumference(radius):
    return math.pi * radius * 2

def get_area(radius):
    return math.pi * radius * radius

def main():
    radius = int(input())

    diameter = get_diameter(radius)
    circumference = get_circumference(radius)
    area = get_area(radius)

    print(radius)
    print(diameter)
    print(circumference)
    print(area)

if __name__ == '__main__':
    main()

after.py


import math

class Circle:
    radius: int
    diameter: int
    circumference: int
    area: int

    def __init__(self, radius):
        self.radius = radius
        self.diameter = radius * 2
        self.circumference = math.pi * radius * 2
        self.area = math.pi * radius * radius

    def get_radius(self):
        return self.radius

    def get_diameter(self):
        return self.diameter

    def get_circumference(self):
        return self.circumference

    def get_area(self):
        return self.area

def main():
    radius = int(input())

    circle = Circle(radius)

    print(circle.get_radius())
    print(circle.get_diameter())
    print(circle.get_circumference())
    print(circle.get_area())

if __name__ == '__main__':
    main()

Rename Variable

before.py


import math

def get_circumference(radius):
    return math.pi * radius * 2

def main():
    hoge = input()
    piyo = get_circumference(hoge)
    print(piyo)

if __name__ == '__main__':
    main()

after.py


import math

def get_circumference(radius):
    return math.pi * radius * 2

def main():
    radius = input()
    circumference = get_circumference(radius)
    print(circumference)

if __name__ == '__main__':
    main()

Introduce Parameter Object

before.py


import math

def print_param(radius, diameter, circumference, area):
    print('radius: {}'.format(radius))
    print('diameter: {}'.format(diameter))
    print('circumference: {}'.format(circumference))
    print('area: {}'.format(area))

def main():
    radius = int(input())
    diameter = radius * 2
    circumference = math.pi * radius * 2
    area = math.pi * radius * radius

    print_param(radius, diameter, circumference, area)

if __name__ == '__main__':
    main()

after.py


import math

class Circle:
    radius: int
    diameter: int
    circumference: int
    area: int

    def __init__(self, radius):
        self.radius = radius
        self.diameter = radius * 2
        self.circumference = math.pi * radius * 2
        self.area = math.pi * radius * radius

    def get_radius(self):
        return self.radius

    def get_diameter(self):
        return self.diameter

    def get_circumference(self):
        return self.circumference

    def get_area(self):
        return self.area

def print_param(circle):
    radius = circle.get_radius()
    diameter = circle.get_diameter()
    circumference = circle.get_circumference()
    area = circle.get_area()

    print('radius: {}'.format(radius))
    print('diameter: {}'.format(diameter))
    print('circumference: {}'.format(circumference))
    print('area: {}'.format(area))

def main():
    radius = int(input())
    circle = Circle(radius)
    print_param(circle)

if __name__ == '__main__':
    main()

Combine Functions into Class

before.py


import math

class Circle:
    radius: int
    diameter: int
    circumference: int
    area: int

    def __init__(self, radius):
        self.radius = radius
        self.diameter = radius * 2
        self.circumference = math.pi * radius * 2
        self.area = math.pi * radius * radius

    def get_radius(self):
        return self.radius

    def get_diameter(self):
        return self.diameter

    def get_circumference(self):
        return self.circumference

    def get_area(self):
        return self.area

def print_param(circle):
    radius = circle.get_radius()
    diameter = circle.get_diameter()
    circumference = circle.get_circumference()
    area = circle.get_area()

    print('radius: {}'.format(radius))
    print('diameter: {}'.format(diameter))
    print('circumference: {}'.format(circumference))
    print('area: {}'.format(area))

def main():
    radius = int(input())
    circle = Circle(radius)
    print_param(circle)

if __name__ == '__main__':
    main()

after.py


class Circle:
    radius: int
    diameter: int
    circumference: int
    area: int

    def __init__(self, radius):
        self.radius = radius
        self.diameter = radius * 2
        self.circumference = math.pi * radius * 2
        self.area = math.pi * radius * radius

    def get_radius(self):
        return self.radius

    def get_diameter(self):
        return self.diameter

    def get_circumference(self):
        return self.circumference

    def get_area(self):
        return self.area

    def print_param(self):
        print('radius: {}'.format(self, radius))
        print('diameter: {}'.format(self, diameter))
        print('circumference: {}'.format(self, circumference))
        print('area: {}'.format(self, area))

def main():
    radius = int(input())
    circle = Circle(radius)
    circle.print_param()

if __name__ == '__main__':
    main()

Combine Functions into Transform

N/A

Split Phase

N/A

Recommended Posts

Refactoring Learned in Python (Basic)
Basic Linear Algebra Learned in Python (Part 1)
Basic sorting in Python
Python classes learned in chemoinformatics
What I learned in Python
I learned Python basic grammar
Character code learned in Python
Python functions learned in chemoinformatics
Scraping with Selenium in Python (Basic)
[Python] Basic knowledge used in AtCoder
I learned about processes in Python
1. Statistics learned with Python 1-1. Basic statistics (Pandas)
Elementary ITK usage learned in Python
Quadtree in Python --2
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
RF Python Basic_01
Meta-analysis in Python
Unittest in python
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
Basic Python writing
N-Gram in Python
Programming in python
Plink in Python
Constant in python
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
Python3 basic grammar
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
RF Python Basic_02
Constant in python
nCr in Python.
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python
Quad-tree in Python
Reflection in Python
Chemistry in Python
Hashable in python
DirectLiNGAM in Python
LiNGAM in Python
Flatten in python
flatten in python
Python variables and data types learned in chemoinformatics