-
Notifications
You must be signed in to change notification settings - Fork 0
Instace Properties
Vinicius Reif Biavatti edited this page Jul 25, 2022
·
4 revisions
- Always use "self" as the first argument
- Do not need to set type hint for "self" variable
- Always use the decorators
@property,@<property>.setterand@<property>.deleterto create properties - Do not use
property()function to create properties - Do not create properties using normal methods
set_something()orget_something()
✅ Do
class Person:
# Getter
@property
def name(self) -> str:
return self.name
# Setter
@name.setter
def name(self, name: str) -> None:
self.name = name
# Deleter
@name.deleter
def name(self) -> None:
del self.name❌ Don't
class Person:
# Using normal method as getter
def get_name(self) -> str:
return self.name
# Using property() function
name = property(fget=..., fset=..., fdel=..., doc=...)- Home
- Structural Naming Conventions
- Format Conventions
- Code Naming Conventions
- Inheritance
- Access Modifiers
- Importation
- Functions and Methods
- Documentation
- Resources