Menu
Forums
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Trending
What's new
New posts
New profile posts
Latest activity
Members
Current visitors
New profile posts
Search profile posts
Upgrades
Log in
Register
What's new
Search
Search
Search titles only
By:
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Menu
Log in
Register
Navigation
Install the app
Install
More options
Contact us
Close Menu
Forums
Software Development
Programming
Tutorials
Writing object oriented Python
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="griimnak" data-source="post: 359764" data-attributes="member: 35695"><p><span style="font-size: 26px">What is Object Oriented Code?</span></p><p>A programming paradigm based on the concept of "<strong>objects</strong>", 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.</p><p></p><p><span style="font-size: 26px">Why should I write Object Oriented Code?</span></p><p>Object Oriented Code makes it easier for you to <strong>implement or remove functions</strong>, <strong>call objects</strong> and <strong>organize your code</strong>. Writing Object Oriented Code is considered good practice, especialy if you're new to the language.</p><p></p><p><span style="font-size: 26px"><strong>Not </strong>Object Oriented:</span></p><p>[PHP]</p><p>#folder.file</p><p>setting1 = 'some text'</p><p>setting2 = 'more text'</p><p>[/PHP]</p><p>[PHP]</p><p>#calling variables example</p><p>import folder.file as config</p><p></p><p>new_var = config.setting1</p><p>another_var = config.setting2[/PHP]</p><p></p><p><span style="font-size: 26px">Object Oriented:</span></p><p>[PHP]</p><p>#folder.file</p><p>class settings(object):</p><p> def __init__(self):</p><p> self.setting1 = 'some text'</p><p> self.setting2 = 'more text'</p><p></p><p> def some_function(self):</p><p> #this is a function</p><p>[/PHP]</p><p>[PHP]</p><p>from folder.file import settings</p><p>config = settings() #store your object in this variable so you can call it</p><p></p><p>new_var = config.setting1</p><p>another_var = config.setting2</p><p>[/PHP]</p><p></p><p><span style="font-size: 26px">What's difference between these two examples?</span></p><ul> <li data-xf-list-type="ul">class settings(object): <br /> Makes an object.<br /> <br /> </li> <li data-xf-list-type="ul">def some_function(self): <br /> Makes a function. self is used to pass the function to the object.<br /> <br /> </li> <li data-xf-list-type="ul">def __init__(self):<br /> 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.<br /> <br /> </li> <li data-xf-list-type="ul">self.setting1 = 'some text'<br /> 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.</li> </ul><p></p><p><span style="font-size: 26px">Online references</span></p><p>OOP Python article: <a href="https://www.jeffknupp.com/blog/2014/06/18/improve-your-python-python-classes-and-object-oriented-programming/" target="_blank">here</a> (I really recommend this)</p><p>Python OOP tutorials: <a href="http://www.python-course.eu/object_oriented_programming.php" target="_blank">here</a></p><p>Offical Python documentation for OOP: <a href="https://docs.python.org/2/tutorial/classes.html" target="_blank">here</a></p><p></p><p>If this helped ya out leave a like, man. cheers.</p></blockquote><p></p>
[QUOTE="griimnak, post: 359764, member: 35695"] [SIZE=7]What is Object Oriented Code?[/SIZE] A programming paradigm based on the concept of "[B]objects[/B]", 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. [SIZE=7]Why should I write Object Oriented Code?[/SIZE] Object Oriented Code makes it easier for you to [B]implement or remove functions[/B], [B]call objects[/B] and [B]organize your code[/B]. Writing Object Oriented Code is considered good practice, especialy if you're new to the language. [SIZE=7][B]Not [/B]Object Oriented:[/SIZE] [PHP] #folder.file setting1 = 'some text' setting2 = 'more text' [/PHP] [PHP] #calling variables example import folder.file as config new_var = config.setting1 another_var = config.setting2[/PHP] [SIZE=7]Object Oriented:[/SIZE] [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] [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 [/PHP] [SIZE=7]What's difference between these two examples?[/SIZE] [LIST] [*]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. [/LIST] [SIZE=7]Online references[/SIZE] OOP Python article: [URL='https://www.jeffknupp.com/blog/2014/06/18/improve-your-python-python-classes-and-object-oriented-programming/']here[/URL] (I really recommend this) Python OOP tutorials: [URL='http://www.python-course.eu/object_oriented_programming.php']here[/URL] Offical Python documentation for OOP: [URL='https://docs.python.org/2/tutorial/classes.html']here[/URL] If this helped ya out leave a like, man. cheers. [/QUOTE]
Insert quotes…
Verification
Post reply
Forums
Software Development
Programming
Tutorials
Writing object oriented Python
Top