Class Explained in JavaScript and Python

What?

A customizable data type.

Why Necesary?

Becuase the built-in data types don't satisfy the needs.

The Design

JavaScript

class Rectangle {
  height = 0; // NOTE: experimental feature (stage 3) - field declaration
  width; // NOTE: ommiting the tailing semicolon is questionable for filed declaration
  #type; // NOTE: private fileds

  constructor(width, height) {
    this.#type = `Rectangle (${width}, ${height})`
    this.width = width // NOTE: "instance" property initialized by assigning `this` in constructor
    this.height = height
  }

  get area() { // NOTE: getter for `.area` defined on .prototype object
    return this.calcArea()
  }

  calcArea() { // NOTE: a method defined on .prototype object
    return this.width * this.height
  }

  static type() { // NOTE: a method defined on this Rectangle object
    return 'Object {} - Reactangle'
  }
}

var rectangle = new Rectangle()
var Rectangle = class {
  // ...
}

var rectangle = new Rectangle()
class Square extends Rectangle {
  constructor (width) {
    super(width, width)
  }
}

Python

TODO: Read https://docs.python.org/3/tutorial/classes.html
TODO: Read https://docs.python.org/3/reference/datamodel.html

Features of class in Python:

a = A()
class Plist(list):

    def __init__(self, l):
        list.__init__(self, l)

    def push(self, item):
        self.append(item)


if __name__ == "__main__":
    x = Plist([3,4])
    x.push(47)
    print(x)
class Length:

    # NOTE: class attribute
    __metric = {"mm" : 0.001, "cm" : 0.01, "m" : 1, "km" : 1000,
                "in" : 0.0254, "ft" : 0.3048, "yd" : 0.9144,
                "mi" : 1609.344 }

    def __init__(self, value, unit = "m" ):
        self.value = value
        self.unit = unit

    def Converse2Metres(self):
        return self.value * Length.__metric[self.unit] # NOTE: call to class attribute

    def __add__(self, other):
        l = self.Converse2Metres() + other.Converse2Metres()
        return Length(l / Length.__metric[self.unit], self.unit )

    def __iadd__(self, other):
        if type(other) == int or type(other) == float:
            l = self.Converse2Metres() + other
        else:
            l = self.Converse2Metres() + other.Converse2Metres()
        self.value = l / Length.__metric[self.unit]
        return self

    def __radd__(self, other):
        return Length.__add__(self, other)  # NOTE: call as class method

    def __str__(self):
        return str(self.Converse2Metres())

    def __repr__(self):
        return "Length(" + str(self.value) + ", '" + self.unit + "')"

if __name__ == "__main__":
    x = Length(4)
    y = eval(repr(x))
    z = Length(4.5, "yd") + Length(1)

    print(x) # 4
    print(repr(z)) # Length(5.593613298337708, 'yd')
    print(z) # 5.1148
import random

class FuzzyTriangleArea:

    def __init__(self, p=0.8, v=0.1):
        self.p, self.v = p, v

    def __call__(self, a, b, c):
        p = (a + b + c) / 2
        result = (p * (p - a) * (p - b) * (p - c)) ** 0.5
        if random.random() <= self.p:
            return result
        else:
            return random.uniform(result-self.v, result+self.v)

area1 = FuzzyTriangleArea()
area2 = FuzzyTriangleArea(0.5, 0.2)
for i in range(12):
    print(f"{area1(3, 4, 5):4.3f}, {area2(3, 4, 5):4.2f}")

References


* cached version, generated at 2020-05-22 15:42:13 UTC.

Subscribe by RSS