-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path17.class.py
More file actions
122 lines (68 loc) · 2.39 KB
/
17.class.py
File metadata and controls
122 lines (68 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
"""
Classes and Objects
Python is an object oriented programming language.
Almost everything in Python is an object, with its properties and methods.
A Class is like an object constructor, or a "blueprint" for creating objects.
"""
# Create a Class
# To create a class, use the keyword class
class Sample:
x = 5
print(Sample)
# Create Object
# Now we can use the class named myClass to create objects
obj = Sample()
print(obj.x)
# The __init__() Function
# The examples above are classes and objects in their simplest form,
# and are not really useful in real life applications.
#
# To understand the meaning of classes we have to understand the built-in __init__() function.
#
# All classes have a function called __init__(), which is always
# executed when the class is being initiated.
#
# Use the __init__() function to assign values to object properties,
# or other operations that are necessary to do when the object is being created
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
person1 = Person("Uma", 36)
print(person1.name)
print(person1.age)
# Note: The __init__() function is called automatically every time
# the class is being used to create a new object.
# Note: The self parameter is a reference to the class instance itself,
# and is used to access variables that belongs to the class.
# Object Methods
# Objects can also contain methods. Methods in objects are functions
# that belongs to the object.
class Student:
def __init__(self, firstname, lastname):
self.firstName = firstname
self.lastName = lastname
def getfullname(self):
return self.firstName + " " + self.lastName
student1 = Student("Umamaheswararao", "Meka")
print(student1.getfullname())
class Vehicle:
def __init__(self, type, color):
self.type = type
self.color = color
def getvehicle(anything):
return anything.type + " " + anything.color
vehical = Vehicle('Brezza', 'red')
print(vehical.getvehicle())
# Modify Object Properties
# You can modify properties on objects like this:
vehical.type = "Vitara Brezza"
print(vehical.getvehicle())
# Delete Object Properties
# You can delete properties on objects by using the del keyword:
# del vehical.color
# print(vehical.getvehicle())
# Delete Objects
# You can delete objects by using the del keyword:
# del vehical
# print(vehical)