Skip to content

Latest commit

 

History

History
181 lines (153 loc) · 2.86 KB

File metadata and controls

181 lines (153 loc) · 2.86 KB

Test case: Class declaration

JavaScript:

class Rectangle {
  constructor(height, width) {
    this.height = height;
    this.width = width;
  }
}

Python:

class Rectangle:

    def __init__(self, height, width):
        self.height = height
        self.width = width

Test case: Class declaration with inheritance

JavaScript:

class Rectangle extends Shape {
  constructor(height, width) {
    super()
    this.height = height;
    this.width = width;
  }
}

Python:

class Rectangle(Shape):

    def __init__(self, height, width):
        super().__init__()
        self.height = height
        self.width = width

Test case: Class declaration access base property

JavaScript:

class Rectangle extends Shape {
  constructor(height, width) {
    this.height = height;
    this.width = width;
    super.color
  }
}

Python:

class Rectangle(Shape):

    def __init__(self, height, width):
        self.height = height
        self.width = width
        super().color

Test case: Class declaration access base method

JavaScript:

class Rectangle extends Shape {
  constructor(height, width) {
    this.height = height;
    this.width = width;
    this.color = super.getColor();
  }
}

Python:

class Rectangle(Shape):

    def __init__(self, height, width):
        self.height = height
        self.width = width
        self.color = super().getColor()

Test case: Anonymous class assigned to a variable

JavaScript:

const Rectangle = class {
  constructor(height, width) {
    this.height = height;
    this.width = width;
  }
};

Python:

class Rectangle:

    def __init__(self, height, width):
        self.height = height
        self.width = width

Test case: Class assigned to a variable

JavaScript:

const Rectangle = class Rectangle2 {
  constructor(height, width) {
    this.height = height;
    this.width = width;
  }
};

Python:

class Rectangle:

    def __init__(self, height, width):
        self.height = height
        self.width = width

Test case: Class with functions

JavaScript:

class Rectangle {
  constructor(height, width) {
    this.height = height;
    this.width = width;
  }

  format(text) {
    return 'new text: ' + text + this.height;
  }

  printText(text) {
    text = this.format(text)
    console.log(text);
  }
};

Python:

class Rectangle:

    def __init__(self, height, width):
        self.height = height
        self.width = width

    def format(self, text):
        return 'new text: ' + text + self.height

    def printText(self, text):
        text = self.format(text)
        console.log(text)

Test case: Class with static method

JavaScript:

class Rectangle {
  static getName() {
    return "Rectangle";
  }
};

Python:

class Rectangle:

    @staticmethod
    def getName():
        return 'Rectangle'