Skip to content

Instantly share code, notes, and snippets.

@Abhayparashar31
Created May 16, 2021 04:37
Show Gist options
  • Save Abhayparashar31/6396eec5b8cded418b0f9a5320a7bcf9 to your computer and use it in GitHub Desktop.
Save Abhayparashar31/6396eec5b8cded418b0f9a5320a7bcf9 to your computer and use it in GitHub Desktop.
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