Created
May 16, 2021 04:37
-
-
Save Abhayparashar31/6396eec5b8cded418b0f9a5320a7bcf9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Employee: | |
| ## class attribute | |
| alias = 'Data Management' | |
| def __init__(self, position,age,salary,experience): | |
| ## Instance attributes | |
| self.position = position | |
| self.age = age | |
| self.salary = salary | |
| self.experience = experience | |
| # instance or object | |
| obj = Employee("Developer", 29,50000,5) | |
| ## accessing class members | |
| print(obj.position) ## accessing self.position | |
| print(obj.age) ## accessing self.age | |
| print(Employee.age) ## Error (Instance attributes only be access by a instance of the class) | |
| print(obj.alias) ## Data Management | |
| print(Employee.alias) ## Data Management (a class attribute can be access by instacen and classs itself) | |
| -----------------------OUTPUT FOR EACH PRINT()------------------------------- | |
| Developer | |
| 29 | |
| AttributeError: type object 'Employee' has no attribute 'age' | |
| Data Management | |
| Data Management |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment