Python Class

Classes in Python...


The following is a defined class in Python. Python uses the naming convention _name_ for internal functions. Python allows operator overloading, e.g. you can define what the operator + will do for a specific class.

_init_ Constructor of the class
_str_ The method which is called if print is applied to this object
_add_ + Operator
_mul_ * Operator

The empty object (null) is called _None_ in Python.

class Point:
    def __init__(self, x=0, y=0):
        self.x = x
        self.y = y
    def __str__(self):
        return "x-value" + str(self.x) + " y-value" + str(self.y)
    def __add__(self,other):
        p = Point()
        p.x = self.x+other.x
        p.y = self.y+other.y
        return p

p1 = Point(3,4)
p2 = Point(2,3)
print (p1)
print (p1.y)
print (p1+p2)
x-value3 y-value4 4 x-value5 y-value7

If you like and would like to contribute, you can also write your article here or mail your article to . See your article appearing on the main page and help others to learn.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Python if , elif and else

Python Conditions and If statements

  • 0
Python for beginners

Learning Python Part 1

  • 3
Struct Alignment and Padding

Struct Alignment and Padding in C++ And C

  • 0
Friend function

Friend function C++

  • 0

C++ Pointers

  • 0

C++ Structures

  • 0
Types of Inheritance in C++

Inheritance and access specifiers C++

  • 0
Java date pattern

Java Date Pattern Syntax

  • 0
Java Date and Calendar

Java Date formats

  • 0
JAVA Data Type

Data types in Java

  • 0
Java unreachable code

Unreachable Code Error in Java

  • 0

Post Comment





Forgot Password

Please enter your email address below and we will send you information to change your password.