Note_Tech

All technological notes.


Project maintained by simonangel-fong Hosted on GitHub Pages — Theme by mattgraham

Python - Classes/Objects

back


Classes/Objects

print("\n--------Create class--------\n")


# pass statement
class Person:
    pass


class Person:

    def __init__(self):
        self.fname = "John"
        self.age = 3

    def printName(self):
        print("print", self.fname, self.age)


print("\n--------Create objects--------\n")
# create an object
p1 = Person()
p2 = Person()
p3 = p2

print("p1:\t", p1)      # p1:      <__main__.Person object at 0x000001920671DE20>

print("p2:\t", p2)      # p2:      <__main__.Person object at 0x000001920671DDC0>
print("p3:\t", p3)      # p3:      <__main__.Person object at 0x000001920671DDC0>


print("\n--------Delete Object--------\n")
del p2
# print("p2:\t", p2)                      # NameError: name 'p2' is not defined
print("p3:\t", p3)                      # p3:      <__main__.Person object at 0x000001DBBD9FDDC0>
# 即以上创建了两个object, p1指向reference一个, p2, p3指向另外一个;
# 删除p2变量, 但另一个对象还被p3指向. 所以调用p2异常, 调用p3正常


print("\n--------Access Properties--------\n")
print("p1.fname:\t", p1.fname)           # John
print("p1.age:\t\t", p1.age)             # 3
# print("p1.lname: ", p1.lname)           # AttributeError: 'Person' object has no attribute 'lname'
# 未创建对象属性时, 不能调用


print("\n--------Change Properties value--------\n")
p1.fname = "Mark"
p1.age = 6

print("p1.fname:\t", p1.fname)           # Mark
print("p1.age:\t\t", p1.age)             # 6

print("p3.fname:\t", p3.fname)           # John
print("p3.age:\t\t", p3.age)             # 3


print("\n--------Create Propertires for an object--------\n")
p1.lname = "Wick"
print("p1.lname: ", p1.lname)           # Wick

print("\n--------Delete Propertires--------\n")
# delete a property from an object
del p1.lname
# print(p1.lname)                           # AttributeError: 'Person' object has no attribute 'lname'

del p1.age
# print(p1.age)                           # AttributeError: 'Person' object has no attribute 'age'


Object Methods


Operators Overloading

print("\n--------Example: Operator overloading--------\n")


class example01:
    def __init__(self, X):
        self.X = X

    # adding two objects
    def __add__(self, U):
        return self.X + U.X


object_1 = example01(int(input("Please enter the value: ")))
object_2 = example01(int(input("Please enter the value: ")))
print(": ", object_1 + object_2)

object_3 = example01(str(input("Please enter the value: ")))
object_4 = example01(str(input("Please enter the value: ")))
print(": ", object_3 + object_4)

# Please enter the value: 23
# Please enter the value: 23
# :  46
# Please enter the value: dfsd
# Please enter the value: fesfe
# :  dfsdfesfe


class example02:
    def __init__(self, X, Y):
        self.X = X
        self.Y = Y

    # Now, we will add the two objects
    def __add__(self, U):
        return self.X + U.X, self.Y + U.Y


Object_1 = example02(23, 12)
Object_2 = example02(21, 22)
Object_3 = Object_1 + Object_2
print(Object_3)         # (44, 34)


class example03:
    def __init__(self, X: int):
        self.X = X

    def __gt__(self, U) -> bool:
        if (self.X > U.X):
            return True
        else:
            return False


object_1 = example03(int(input(("Please enter the value: "))))
object_2 = example03(int(input(("Please enter the value: "))))
if (object_1 > object_2):
    print("The object_1 is greater than object_2")
else:
    print("The object_2 is greater than object_1")
# Please enter the value: 11
# Please enter the value: 22
# The object_2 is greater than object_1


class example04:
    def __init__(self, X):
        self.X = X

    def __lt__(self, U):
        if (self.X < U.X):
            return "object_1 is less than object_2"
        else:
            return "object_2 is less than object_1"

    def __eq__(self, U):
        if (self.X == U.X):
            return "Both the objects are equal"
        else:
            return "Objects are not equal"


object_1 = example04(int(input("Please enter the value: ")))
object_2 = example04(int(input("Please enter the value: ")))
print(": ", object_1 < object_2)
# Please enter the value: 11
# Please enter the value: 23
# :  object_1 is less than object_2


object_3 = example04(int(input("Please enter the value: ")))
object_4 = example04(int(input("Please enter the value: ")))
print(": ", object_3 == object_4)
# Please enter the value: 1
# Please enter the value: 1
# :  Both the objects are equal

Operator Magic Function  
     
- add(self, other)  
* sub(self, other)  
- mul(self, other)  
/ truediv(self, other)  
// floordiv(self, other)  
% mod(self, other)  
** pow(self, other)  
>> rshift(self, other)  
<< lshift(self, other)  
& and(self, other)  
` ` or(self, other)
^ xor(self, other)  
Operator Magic Function
< LT(SELF, OTHER)
> GT(SELF, OTHER)
<= LE(SELF, OTHER)
= GE(SELF, OTHER)
== EQ(SELF, OTHER)
!= NE(SELF, OTHER)
Operator Magic Function  
-= ISUB(SELF, OTHER)  
+= IADD(SELF, OTHER)  
*= IMUL(SELF, OTHER)  
/= IDIV(SELF, OTHER)  
//= IFLOORDIV(SELF, OTHER)  
%= IMOD(SELF, OTHER)  
**= IPOW(SELF, OTHER)  
= IRSHIFT(SELF, OTHER)  
<<= ILSHIFT(SELF, OTHER)  
&= IAND(SELF, OTHER)  
` =` IOR(SELF, OTHER)
^= IXOR(SELF, OTHER)  
Operator Magic Function
* NEG(SELF, OTHER)
- POS(SELF, OTHER)
~ INVERT(SELF, OTHER)

Class variable

print("\n-------- Class variable  --------\n")


class Person:
    count = 0   # a class variable

    def __init__(self, name, age):
        self.name = name  # an instance variable
        self.age = age
        Person.count += 1   # Call the class variable using the name of the class


person1 = Person("Ayan", 25)
person2 = Person("Bobby", 30)
print(Person.count)  # 2

TOP