<object-name> = <class-name>(<arguments>)
Python objects examples
Simple example code created the object of MyClass class.
class MyClass: var = 10 # Creating object obj = MyClass() print(obj) print(type(obj))
Accessing variable using object
The variables that belong to a class and are always public, it can be accessed using the dot (.) operator.
class MyClass: var = 10 test = MyClass() print(test.var)
Python object access method
This example created an Employee class that consisted of two attributes name, salary, and display function. We created the object of the Employee class called emp1. Using the object along with the .dot operator, we accessed the class function.
class Employee: def __init__(self, name, salary): self.name = name self.age = salary def my_func(self): print("Name " + self.name) emp1 = Employee("John", 36000) emp1.my_func()
Output: Name John
Do comment if you have any doubts and suggestions on this Python basic tutorial.
Note: IDE: PyCharm 2021.1.3 (Community Edition)
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.
Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical.