Writing Object Oriented Python
What is Object Oriented Code?
A programming paradigm based on the concept of "objects", which are data structures that contain data, in the form of fields, often known as attributes; and code, in the form of procedures, often known as methods.
Why should I write Object Oriented Code?
Object Oriented Code makes it easier for you to implement or remove functions, call objects and organize your code. Writing Object Oriented Code is considered good practice, especialy if you're new to the language.
Not Object Oriented:
Object Oriented:
What's difference between these two examples?
class settings(object):
Makes an object.
def some_function(self):
Makes a function. self is used to pass the function to the object.
def __init__(self):
This one is interesting. Anything that you'd want to call from the class itself from another location would go under this function. It makes whatever is under that function public.
self.setting1 = 'some text'
Why the self.? If you were to just put setting1 under the __init__ function it wouldn't work, because setting1 is just a local variable. Adding self. to it makes it bound to that object, thus allowing you to call it elsewhere.
Online references
OOP Python article: here (I really recommend this)
Python OOP tutorials: here
Offical Python documentation for OOP: here
If this helped ya out leave some rep, man. cheers.
A programming paradigm based on the concept of "objects", which are data structures that contain data, in the form of fields, often known as attributes; and code, in the form of procedures, often known as methods.
Why should I write Object Oriented Code?
Object Oriented Code makes it easier for you to implement or remove functions, call objects and organize your code. Writing Object Oriented Code is considered good practice, especialy if you're new to the language.
Not Object Oriented:
Code:
#folder.file setting1 = 'some text' setting2 = 'more text'
Code:
#calling variables example import folder.file as config new_var = config.setting1 another_var = config.setting2
Code:
#folder.file
class settings(object):
def __init__(self):
self.setting1 = 'some text'
self.setting2 = 'more text'
def some_function(self):
#this is a function
Code:
from folder.file import settings config = settings() #store your object in this variable so you can call it new_var = config.setting1 another_var = config.setting2
class settings(object):
Makes an object.
def some_function(self):
Makes a function. self is used to pass the function to the object.
def __init__(self):
This one is interesting. Anything that you'd want to call from the class itself from another location would go under this function. It makes whatever is under that function public.
self.setting1 = 'some text'
Why the self.? If you were to just put setting1 under the __init__ function it wouldn't work, because setting1 is just a local variable. Adding self. to it makes it bound to that object, thus allowing you to call it elsewhere.
Online references
OOP Python article: here (I really recommend this)
Python OOP tutorials: here
Offical Python documentation for OOP: here
If this helped ya out leave some rep, man. cheers.