My QA Projects

QA Projects I was involded.

View on GitHub

Creating Objects (Instances)

Create objects using Constructors

Syntax

class ClassName:
    def __init__(self, parameter1, parameter2, ...):
        # Initialize instance variables
        self.parameter1 = parameter1
        self.parameter2 = parameter2
        # Add more initialization as needed

# Creating an object of the class
object_name = ClassName(value1, value2, ...)

Example 1

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

# Creating an object of the class Person
person1 = Person("Alice", 30)
print(person1.name)  # Output: Alice
print(person1.age)   # Output: 30

Example Rectangle Constructor

create a rectangle object with width and height

# Instantiation Example

class Rectangle:
    def __init__(self, width, height):
        self.width = width
        self.height = height
    def area(self):
        return self.width*self.height
# he will explain the class defintion later
# this is just the instantiation

r1 = Rectangle(200,100)

when we call the rectangle constructor with a width of 200 and 100 it will instantiate and return a rectangle object with those dimensions

Accessing Instance Variables

obj.myvar

# the rectangle example we have two properties
rect.width # the width of a rectangle object rect
rect.height
rect.area() # return the area of a rectangle object rect

Instance Variables Example

#to access the instance variable
r1 = Rectangle (200,100)
print(r1.width)
#Output 200
print(r1.height)
#Output 100

Invoking Instance Methods

obj.mymethod(arg1,arg2)

Instance Method Example

r1 = Rectangle(200,100)
print(r1.area())
# Output 20000

Copying Objects

variable = Class()

Copying Objects Example 1

r1 = Rectangle(200,100)
r3 = r1
r3.width = 150
print(r3.width, r3.area())
# Output
150 15000
r1 = Rectangle(200,100)
r3 = r1
r3.width = 150
print(r3.width, r3.area())
print(r1.width, r1.area())
# Output
150 15000
150 15000

Identity Function

lokking at an objects identity by using the id function

id(obj)

return obj’s identity

Copying Objects Example 2

r1 = Rectangle(200,100)
r3 = r1
# we have not create a new object, we just referencing r1 from r3; any changes to r3 affect r1

r3.width = 150
print(id(r1)==id(r3))
# Output
True
r3= Rectangle(200,100)
print(id(r1)==id(r3))
# Output
False
r3.height = 75
print(r.height, r3.height)
# Output
100 75

Object Oriented Design docs special methods docs obect init