Mit Python erlerntes Refactoring (Basic)

Funktion extrahieren

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-Funktion

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

Variable extrahieren

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

Funktionserklärung ändern

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

Variable einkapseln

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

Variable umbenennen

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

Parameterobjekt einführen

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

Funktionen in Klasse kombinieren

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

Kombinieren Sie Funktionen zu Transform

N/A

Geteilte Phase

N/A

Recommended Posts

Mit Python erlerntes Refactoring (Basic)
Grundlegende Sortierung in Python
Python-Kurs zum Lernen mit Chemoinfomatik
Was ich in Python gelernt habe
Ich habe die grundlegende Python-Grammatik gelernt
In Python gelernter Zeichencode
Python-Funktionen mit Chemoinfomatik gelernt
Scraping mit Selen in Python (Basic)
[Python] Grundkenntnisse in AtCoder
Ich habe versucht, den Prozess mit Python zu studieren
1. Mit Python 1-1 gelernte Statistiken. Grundlegende Statistiken (Pandas)
Grundlegende ITK-Verwendung mit Python gelernt
Quadtree in Python --2
Python in der Optimierung
CURL in Python
Metaprogrammierung mit Python
Python 3.3 mit Anaconda
Geokodierung in Python
SendKeys in Python
RF Python Basic_01
Metaanalyse in Python
Unittest in Python
Epoche in Python
Zwietracht in Python
Deutsch in Python
DCI in Python
Quicksort in Python
nCr in Python
Python-Grundschrift
N-Gramm in Python
Programmieren mit Python
Plink in Python
Konstante in Python
FizzBuzz in Python
SQLite in Python
Schritt AIC in Python
Grundlegende Grammatik von Python3
LINE-Bot [0] in Python
CSV in Python
Reverse Assembler mit Python
Reflexion in Python
RF Python Basic_02
Konstante in Python
nCr in Python.
Format in Python
Scons in Python 3
Puyopuyo in Python
Python in Virtualenv
PPAP in Python
Quad-Tree in Python
Reflexion in Python
Chemie mit Python
Hashbar in Python
DirectLiNGAM in Python
LiNGAM in Python
In Python reduzieren
In Python flach drücken
Python-Variablen und Datentypen, die mit Chemoinfomatik gelernt wurden