Refactoring appris avec Python (Basic)

Fonction d'extraction

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()

Fonction en ligne

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()

Extraire la 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()

Variable en ligne

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()

Modifier la déclaration de fonction

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()

Encapsuler la 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()

Renommer la 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()

Introduire un objet de paramètre

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()

Combiner les fonctions en classe

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()

Combiner les fonctions en transformation

N/A

Phase de division

N/A

Recommended Posts

Refactoring appris avec Python (Basic)
Tri de base en Python
Classe Python pour apprendre avec la chimioinfomatique
Ce que j'ai appris en Python
J'ai appris la grammaire de base de Python
Code de caractère appris en Python
Fonctions Python apprises avec la chimioinfomatique
Scraping avec Selenium en Python (Basic)
[Python] Connaissances de base utilisées dans AtCoder
J'ai essayé d'étudier le processus avec Python
1. Statistiques apprises avec Python 1-1. Statistiques de base (Pandas)
Utilisation élémentaire d'ITK apprise avec Python
Quadtree en Python --2
Python en optimisation
CURL en Python
Métaprogrammation avec Python
Python 3.3 avec Anaconda
Géocodage en python
SendKeys en Python
RF Python Basic_01
Méta-analyse en Python
Unittest en Python
Époque en Python
Discord en Python
Allemand en Python
DCI en Python
tri rapide en python
nCr en python
Écriture de base Python
N-Gram en Python
Programmation avec Python
Plink en Python
Constante en Python
FizzBuzz en Python
Sqlite en Python
Étape AIC en Python
Grammaire de base Python3
LINE-Bot [0] en Python
CSV en Python
Assemblage inversé avec Python
Réflexion en Python
RF Python Basic_02
Constante en Python
nCr en Python.
format en python
Scons en Python 3
Puyopuyo en python
python dans virtualenv
PPAP en Python
Quad-tree en Python
Réflexion en Python
Chimie avec Python
Hashable en Python
DirectLiNGAM en Python
LiNGAM en Python
Aplatir en Python
Aplatir en python
Variables Python et types de données appris avec la chimio-automatique