Ein einfaches Python-Beispiel für Klassen und Funktionen

Hier noch ein einfaches Python-Beispiel für Klassen und Funktionen, das ich vor ein paar Tagen geschrieben habe. Die Punkt-Klasse erhält eine entsprechende Funktion, um die Euklidische Distanz zu einem anderen Punkt zu bestimmen.

# -*- coding: utf-8 -*-
import math as m
 
class Point:
 
    def __init__(self,x,y):
        self.x = x
        self.y = y
 
    def calcEuclidDistanceToPoint(self,x,y):
        return m.sqrt(m.pow(self.x-x,2) + m.pow(self.y-y,2))
 
p1 = Point(0,0)
p2 = Point(1,1)
print(p2.calcEuclidDistanceToPoint(p1.x,p1.y))
runfile('euclidDistance.py', wdir='E:/Python')
1.4142135623730951

Uwe

Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined. Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.

More Posts - Website