0% found this document useful (0 votes)
8 views2 pages

Python Class and Object Examples

The document contains Python code defining several classes including 'car', 'toyotacar', 'cruiser', 'A', 'B', and 'C', showcasing inheritance and static methods. It also includes a 'student' class with a method to change the student's name and a property to calculate the percentage of marks. The code demonstrates object instantiation and method calls, along with the use of class variables and properties.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views2 pages

Python Class and Object Examples

The document contains Python code defining several classes including 'car', 'toyotacar', 'cruiser', 'A', 'B', and 'C', showcasing inheritance and static methods. It also includes a 'student' class with a method to change the student's name and a property to calculate the percentage of marks. The code demonstrates object instantiation and method calls, along with the use of class variables and properties.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

print("Try programiz.

pro")
class car:
color = "black"
@staticmethod
def start():
print("car started")
@staticmethod
def stop():
print("car stoped")

class toyotacar(car):
def __init__(self, origin):
[Link] = origin

class cruiser(toyotacar):
def __init__(self, type, milege, control):
# super().__init__(origin)
[Link] = type
[Link] = milege
[Link] = control

car1 = cruiser("diesel", "24kmpl", "semi-auto")


car2 = cruiser("petrol", "20kmpl", "manual")

# print([Link])
print([Link]())

class A:
varA = "welcome to class A"

class B:
varB = "welcome to vlass B"

class C(A, B):


varC = "welcome to class C"

c1 = C()
c2 = C()

print([Link])
print([Link])

class student:
name = "rahul"

@classmethod
def changename(cls, name):
[Link] = name

s1 = student()
s2 = student()
[Link]("anonymous")
print([Link])
print([Link])
class student:
def __init__(self, phy, chem, maths):
[Link] = phy
[Link] = chem
[Link] = maths

@property
def cal_percentage(self):
return str([Link] + [Link] + [Link] / 3) + " %"

s1 = student(76, 84, 86)


[Link] = 86
print([Link])
print(s1.cal_percentage)

Common questions

Powered by AI

The output of the line print(s1.cal_percentage) is '168.66666666666666 %'. This is because the 'cal_percentage' property is calculated as the sum of 'phy', 'chem', and 'maths' divided by 3, followed by concatenation with ' %'. Given 'phy' is changed to 86, the calculation becomes (86 + 84 + 86)/3, which equals 168.666... The value is then converted to a string and appended with ' %', explaining the output .

There will be no functional difference when 'cruiser' instances 'car1' and 'car2' call the 'stop()' method. This is because 'stop()' is a static method defined in the 'car' class and inherits automatically. It operates independently of instance-specific attributes such as 'type', 'milege', or 'control'. Consequently, 'stop()' will produce the same output, 'car stopped', for both instances, as it does not utilize any instance data .

In the provided code, class-level attributes like 'name' in 'student' or 'varA' in class 'A' are shared across all instances. Instance-level attributes are individualized per object instance, like 'phy', 'chem', and 'maths' in 'student'. However, reliance on class-level attributes can introduce issues where changes in one instance (when improperly configured) affect all instances, as seen with 's1.changename("anonymous")' affecting 'student.name'. Additionally, potential errors can arise when inherited attributes like 'origin' in 'cruiser' are not properly initialized, leading to runtime errors .

The class 'C' implements multiple inheritance by inheriting from both classes 'A' and 'B'. This allows 'C' to access attributes from both parent classes, such as 'varA' from 'A' and 'varB' from 'B'. The main benefit of this feature is the ability to combine functionality and data from several classes, leading to flexible and reusable code structures. This kind of inheritance can be complex, as it requires careful management to avoid conflicts and ensure all necessary attributes are accessible and correctly initialized .

The 'cruiser' class is intended to include an 'origin' attribute inherited from 'toyotacar', but it omits calling 'super().__init__(origin)' in its constructor. This could lead to an AttributeError if an attempt is made to access 'origin' for a 'cruiser' object, as this attribute is not initialized. Furthermore, assigning default values for attributes 'type', 'milege', and 'control' within the 'cruiser' constructor without properly handling 'origin' may cause confusion and make the class less robust for extension or modification .

Static methods in Python are defined with the '@staticmethod' decorator and do not require an object instance to be called. In the 'car' class, 'start()' and 'stop()' are static methods, which means they can be invoked directly from the class without the need to instantiate an object. This design choice is effective for actions associated with the class that do not involve data specific to an instance, such as printing 'car started' and 'car stopped', which are general actions applicable to any car context .

The '@classmethod' decorator in Python allows a method to be called on the class itself, not on a specific instance of the class. In the 'student' class, the 'changename' method is defined with '@classmethod', allowing it to modify the class attribute 'name' directly. When 'changename' is called on an instance 's1', it changes the 'name' attribute for the entire class. Therefore, both 's1.name' and 'student.name' reflect the change, showing 'anonymous' instead of the initial value 'rahul' .

Both print(s1.name) and print(student.name) output 'anonymous' because the 'changename' method is a class method and is called on 's1', an instance of 'student'. Class methods can change class-level attributes, which in this case is 'name'. The call modifies the 'name' attribute for the 'student' class itself, not just for the instance. This is because class methods operate on the class as a whole, affecting all instances and the class attribute directly .

To correctly calculate the average percentage of the marks in the 'student' class's 'cal_percentage' method, the function should be altered to squaring the sum of all subject marks and dividing by the correct number of subjects (3), rather than dividing the marks of 'maths' alone by 3. The corrected function would look like this: return str((self.phy + self.chem + self.maths) / 3) + ' %'. This would yield the true average percentage of the subjects' scores .

The 'cruiser' class inherits from 'toyotacar', which in turn inherits from 'car'. This structure enables 'cruiser' to access methods and attributes defined in 'car' and 'toyotacar'. For example, 'cruiser' can call methods like 'start()' and 'stop()', provided by 'car', without needing to redefine them. Although the 'origin' attribute from 'toyotacar' is intended to be initialized in 'cruiser', it is not properly integrated due to the missing super().__init__(origin) call in its constructor. This omission leads to potential AttributeError if 'origin' were accessed, as it is not initialized in the 'cruiser' instances .

You might also like