I've tried looking around online but I can't find a source that seems to explain it well enough for me - so maybe one of y'all could help out? I get that classes are like templates for creating an object, but whats the difference between an object and a variable? Are they the same, and if so why not use one word or the other?
|
Python
Can't seem to grasp the concept of Classes and Objects. |
|
Dec 3, 2019 08:25 PM
(This post was last modified: Dec 3, 2019 08:39 PM by lnvisible.)
Think of variables as containers. Within those containers you are able to store data values.
Code x = 22Python is an OOP (Object Oriented Programming) language so pretty much everything is an object. A class is like the main ingredient for finishing a recipe, and in this case a class creates an object. Code class TestClass:
(Dec 3, 2019 08:25 PM)lnvisible Wrote: Think of variables as containers. Within those containers you are able to store data values. I think I'm starting to grasp it. In what real world situation would you need to use this? What's the point of putting x in TestClass if you could just print x?
Dec 3, 2019 10:08 PM
(This post was last modified: Dec 3, 2019 10:17 PM by gigajew.)
A class is like a description of an object type. If I wanted my app to understand what an apple is I can first create an interface named Fruit and then inherit from that and create a class called Apple. My fruit interface may describe a string property that I can set to describe the flavor.
Here's an example in C#: And another in Python (not my strong language) ![]()
Dec 3, 2019 10:12 PM
(This post was last modified: Dec 3, 2019 10:12 PM by grady.)
(Dec 3, 2019 10:08 PM)gigajew Wrote: A class is like a description of an object type. If I wanted my app to understand what an apple is I can first create an interface named Fruit and then inherit from that and create a class called Apple. My fruit interface may describe a string property that I can set to describe the flavor. Example: This is the Python SF try not to be confusing with other language's syntax (Dec 3, 2019 10:12 PM)grady Wrote: This is the Python SF Sorry just realized added a python example, perhaps some insight into other languages could strengthen his concept of a class some ![]()
Dec 3, 2019 10:27 PM
(This post was last modified: Dec 3, 2019 10:28 PM by realsunjester.)
(https://docs.python.org/3/tutorial/classes.html)
alright man, classes are easy. classes are "objects". you can make a class our of any real world object. for example: Apple (thats a thing/object). Apples have color right? so... Code >>> class Apple:so another example, vehicles. Code >>> class Vehicle:gnome sayin? basically people create classes so you dont have to type code over and over again, its "reusability" and also makes it possible to edit code without having to find every instance of something, you could add or modify the class. ---- (https://docs.python.org/3/tutorial/classes.html) alright man, classes are easy. classes are "objects". you can make a class our of any real world object. for example: Apple (thats a thing/object). Apples have color right? so... Code >>> class Apple:so another example, vehicles. Code >>> class Vehicle:gnome sayin?
http://sunjester.rf.gd - proxies, logins, scanner results
(Dec 3, 2019 10:19 PM)gigajew Wrote: Sorry just realized added a python example, perhaps some insight into other languages could strengthen his concept of a class some ye np fam, I almost made the same mistake myself so i feel u also @ OP It makes more sense when you have data that makes sense to be encapsulated in a class it's very possible to create pure function to manage the data in a program let's go with a chess board for example You can see how much cleaner the code is when implementing objects instead of just functions
Dec 3, 2019 10:53 PM
(This post was last modified: Dec 3, 2019 11:09 PM by koshka1.)
Here are my two cents:
(Dec 3, 2019 09:42 PM)pkdarn Wrote: I think I'm starting to grasp it. In what real world situation would you need to use this? What's the point of putting x in TestClass if you could just print x? You're right there's no point in putting x in TestClass, however in the real world when you are making applications that are much bigger you will have to worry about memory management. The stack and the heap the stack is automatically allocated by the compiler, for example if you wrote int x = 1; it would be stored on the stack and when the program is done it would be automatically deleted. On the flip side the heap is explicitly controlled by the programmer, so if I created an object: Classname c = new Classname(); the object © is on the heap, and it is waiting to be destroyed. In C++ you just say delete c and you're all good. I probably didn't do a great job of answering your question but I recommend you looking into data structures like linked lists, stacks, queues etc it might make more sense when you see the organization of data along with OOP I wanted to add an example: Imagine you are the manager of a store and you want to manage items in your store. If you were to approach this without object-oriented programming you'd quickly realize the program itself will become extremely bulky. ![]() PHP Code This simple example demonstrates inheritance, you have a parent class FOOD and children classes sweet, meat, starch etc. I can now create different food objects and do what I want with them, like stock the store. Quote:basically people create classes so you dont have to type code over and over again, its "reusability" and also makes it possible to edit code without having to find every instance of something, you could add or modify the class. I like what this person said, see how the class children of food all have the same data members? I can organize and reuse the code efficiently because of OOP. Feels good to shed a little bit of knowledge to some younger people after suffering in my data structures class
Dec 3, 2019 11:59 PM
(This post was last modified: Dec 4, 2019 12:01 AM by 0xDEAD10CC.)
(Dec 3, 2019 10:53 PM)koshka1 Wrote: Here are my two cents: Why somebody would use an enum like that in addition to polymorphism is beyond me though... Makes no sense to do that. You've also only probably confused people with your C++ example in a Python forum.
Getting Started: C/C++
[C/C++/.NET/x86 ASM - Automation Programmer] (Dec 3, 2019 11:59 PM)0xDEAD10CC Wrote: Why somebody would use an enum like that in addition to polymorphism is beyond me though... Makes no sense to do that. You've also only probably confused people with your C++ example in a Python forum. It was a project and using the enum 'like that' was apart of the specifications. Imo the code is so similar that it really doesn't matter what it is, hell we could even do psuedocode if it's confusing OP. I do concede though that it would probably be best to show these concepts through pictures, I figured the UML was enough but maybe not...
Dec 4, 2019 12:19 AM
(This post was last modified: Dec 4, 2019 12:20 AM by 0xDEAD10CC.)
(Dec 4, 2019 12:12 AM)koshka1 Wrote: It was a project and using the enum 'like that' was apart of the specifications. Diagrams are good, but OP's question is too broad and vague to answer accurately. The use of classes, which promotes encapsulation among other possibilities could spark conversation about framework extensibility and avoiding breaking changes for instance... That alone is already a huge topic, so I'm confused as to what OP is even really asking here... It would be better if he explained which parts he didn't understand than to just tell us that he doesn't understand it.
Getting Started: C/C++
[C/C++/.NET/x86 ASM - Automation Programmer]
Dec 4, 2019 01:01 AM
(This post was last modified: Dec 4, 2019 01:02 AM by Lindros.)
(Dec 3, 2019 09:42 PM)pkdarn Wrote: I think I'm starting to grasp it. In what real world situation would you need to use this? What's the point of putting x in TestClass if you could just print x? Variables are absolutely necessary for storing things that are returned to use later. They are good for abstracting things. You should learn more basic syntax or "grammar" of the language before worrying too much about classes. I'd recommend actually learning more about functions first then dive into classes. Python is an "object oriented" programming language so basically everything is an object or an instance of a class. Basically a string even is just an instance of a class previously defined elsewhere is Pythons default libraries. This is because Python is a higher level, interpreted language. I know you might know some of these statements but sometimes you need to repeat things until you start getting that internal picture in your mind. (Dec 4, 2019 12:19 AM)0xDEAD10CC Wrote: Diagrams are good, but OP's question is too broad and vague to answer accurately. The use of classes, which promotes encapsulation among other possibilities could spark conversation about framework extensibility and avoiding breaking changes for instance... That alone is already a huge topic, so I'm confused as to what OP is even really asking here... It would be better if he explained which parts he didn't understand than to just tell us that he doesn't understand it. I agree with this, I think the OP is trying to dive into the concepts too quickly before breaking down and internalizing the basics. ![]()
Dec 4, 2019 04:18 AM
(This post was last modified: Dec 4, 2019 04:28 AM by Money-.)
To put it simple though, the whole point of OOP concept is code reusability and maintainability.
An object is created via a class with user defined data types and attributes. Comparing it to variables and you only declare them once then you can assign how many data you want on an array without so many work as compared to variables. Example, you are making a sales program. You have 5 customers. Using Variable Concept. You will obviously create 5 customer variables with 5 names , 5 Customer IDs and 5 emails variables right? Ain't that very tedious? So this is where object oriented concept comes into play. Using Objects built via Class. You will create a class called Customer with attributes called name , ID , email , phone number etc. So now you will only create 1 object of TYPE Customer or an array object of TYPE Customer with size 5 aka which is 1 line of code to assign 5 customers data such as name , email etc. without declaring 10 or 20 variables just to achieve that. I hope I answered your question. I assume you are asking what's the benefits of objects compared to variable or how they are different. I ain't good at writing a good explanation but I am just explaining what I understand. Edit : If you really want to fully understand how object-oriented concept works. I highly recommend you to play around with Java Language instead of Python. I learned my OOP concept purely via Java.
Object Oriented Programming is more than just knowing how to make a class or instantiate an object.
Also I would STRONGLY recommend that you learn this in and out with a Java first before applying it to Python or even C++. PM me if you want to really learn how it works I am extremely proficient in this paradigm of software development. |
|
|



![[Image: Syr3DlV.png]](https://i.imgur.com/Syr3DlV.png)
![[Image: rFtJcUv.png]](https://i.imgur.com/rFtJcUv.png)
![[Image: Iu0EfTL.gif]](https://i.imgur.com/Iu0EfTL.gif)

![[Image: j90qyxk.gif]](https://i.imgur.com/j90qyxk.gif)