- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is the difference between attributes and properties in python?
In python, everything is an object. And every object has attributes and methods or functions. Attributes are described by data variables for example like name, age, height etc.
Properties are special kind of attributes which have getter, setter and delete methods like __get__, __set__ and __delete__ methods.
However, there is a property decorator in Python which provides getter/setter access to an attribute Properties are a special kind of attributes. Basically, when Python encounters the following code:
foo = SomeObject() print(foo.bar)
it looks up bar in foo, and then examines bar to see if it has a __get__, __set__, or __delete__ method and if it does, it's a property. If it is a property, instead of just returning the bar object, it will call the __get__ method and return whatever that method returns.
In Python, you can define getters, setters, and delete methods with the property function. If you just want the read property, there is also a @property decorator you can add above your method.
class C(object): def __init__(self): self._x = None #C._x is an attribute @property def x(self): """I'm the 'x' property.""" return self._x # C._x is a property This is the getter method @x.setter # This is the setter method def x(self, value): self._x = value @x.deleter # This is the delete method def x(self): del self._x
- Related Articles
- What is the difference between novalidate and formnovalidate attributes?
- What is the difference between id and name attributes in HTML?
- What is the difference between “lang” and “type” attributes in a script tag?
- What is the difference between FromBody and FromUri attributes in C# ASP.NET WebAPI?
- Difference between Synthesized and Inherited Attributes
- What is the difference between = and == operators in Python?
- What is the difference between the != and operators in Python?
- What is the difference between os.open and os.fdopen in python?
- What is the difference between dict.items() and dict.iteritems() in Python?
- What is the difference between __str__ and __repr__ in Python?
- What is the difference between arguments and parameters in Python?
- What is the difference between single and double quotes in python?
- What is the difference between global and local variables in Python?
- What is the difference between root.destroy() and root.quit() in Tkinter(Python)?
- What is the difference between Python functions datetime.now() and datetime.today()?
