Writing object oriented Python

griimnak

You're a slave to the money then you die
Jul 20, 2013
957
800
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:
PHP:
#folder.file
setting1 = 'some text'
setting2 = 'more text'
PHP:
#calling variables example
import folder.file as config

new_var = config.setting1
another_var = config.setting2

Object Oriented:
PHP:
#folder.file
class settings(object):
     def __init__(self):
          self.setting1 = 'some text'
          self.setting2 = 'more text'

     def some_function(self):
          #this is a function
PHP:
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

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: (I really recommend this)
Python OOP tutorials:
Offical Python documentation for OOP:

If this helped ya out leave a like, man. cheers.
 
Last edited:

Jemz

xmas tings.
Aug 6, 2013
166
17
Interesting read, wouldn't mind learning Python one day considering how widely used it is.
 

griimnak

You're a slave to the money then you die
Jul 20, 2013
957
800
Mind explaining the pros and cons of using Python over something like PHP (assuming that Python is used for web development)
Python is actualy not used for web development as often as it's used for actual applications. Someday i'll make a thread on that topic.
Interesting read, wouldn't mind learning Python one day considering how widely used it is.
Cheers man, I really recommend you learn python since it's cross platform and can be ran from windows, linux, arduino you name it.
 

LeChris

https://habbo.codes/
Sep 30, 2013
2,786
1,395
Python is actualy not used for web development as often as it's used for actual applications. Someday i'll make a thread on that topic.

Cheers man, I really recommend you learn python since it's cross platform and can be ran from windows, linux, arduino you name it.
But PHP is cross-platform, and can be set up and ran faster as well as the ability to develop without having to compile code
 

TesoMayn

Boredom, it vexes me.
Oct 30, 2011
1,482
1,482
But PHP is cross-platform, and can be set up and ran faster as well as the ability to develop without having to compile code
Wasn't going to get into this topic until you posted that

Do you have any proof that PHP is faster than Python? Have YOU ran any test yourself to prove this?
 

griimnak

You're a slave to the money then you die
Jul 20, 2013
957
800
Python (in web dev) by default might not be as fast, but python could maybe push limits because since it's a python application you can setup threading with a module. I'll have to look into that.
php requires a webserver though.

Now, in web development php wins without a doubt. It's the most supported language for web development and it has been for years. python in general though, you can literaly run it wherever the fuk you want :p
 

Jemz

xmas tings.
Aug 6, 2013
166
17
Surely you are able to create a back-end for a website using Python, I know Google & Twitch have.
 

BIOS

ಠ‿ಠ
Apr 25, 2012
906
247
Surely you are able to create a back-end for a website using Python, I know Google & Twitch have.
..................................................
You obviously can, even the previous replies hint towards that.
 

Adil

DevBest CEO
May 28, 2011
1,278
716
But PHP is cross-platform, and can be set up and ran faster as well as the ability to develop without having to compile code
Python is interpreted, like PHP.

Also, muh out of context benchmarks!!!!!!
 

Jemz

xmas tings.
Aug 6, 2013
166
17
..................................................
You obviously can, even the previous replies hint towards that.
But if PHP is superior in web development compared to Python, why are large sites like Twitch and Google using it?
 

griimnak

You're a slave to the money then you die
Jul 20, 2013
957
800
But if PHP is superior in web development compared to Python, why are large sites like Twitch and Google using it?
It depends, man. PHP is the most supported, but it doesn't really mean it's "the greatest".

Python is interpreted, like PHP.

Also, muh out of context benchmarks!!!!!!
I didn't even know PHP was an interpreter language like python :eek: the more you know..
 

LeChris

https://habbo.codes/
Sep 30, 2013
2,786
1,395
Google uses a variety of coding languages for it's services, not just stand alone Python
 

Users who are viewing this thread

Top