Function being an object

I watched some youtube videos about functions. one of the videos said that functions are objects. Then what is a class? Class is essentially a group of methods, which are objects. Then what is holding them together? Is a class stored in a memory?

You must select a tag to post in this category. Please find the tag relating to the section of the course you are on E.g. loops, learn-compatibility

When you ask a question, don’t forget to include a link to the exercise or project you’re dealing with!

If you want to have the best chances of getting a useful answer quickly, make sure you follow our guidelines about how to ask a good question. That way you’ll be helping everyone – helping people to answer your question and helping others who are stuck to find the question and answer! :slight_smile:

To my knowledge:
A function is a tool used to re-use multiple lines of code without having to write the whole thing out all the time.

Yes, a class is an object. Yes, a class can store other objects. I do not know about the whole memory thing (That reminds me that I need to study that) but I do know that when it comes to classes and their objects, classes don’t “contain” objects. They point to objects. Whereas things like characters and numbers are stored as bytes. (What happens in that realm is beyond me)

As for a classes being stored in memory, every time you start up your program, the computer reads the code to create the class object again using your instructions. It doesn’t store the class object in memory to pick it up again. Well … there are modules in python that allow you do store class objects in memory. Anyway!!

I understand your confusion. The main take away is that classes are objects that are an organizational tool for OOP. They can point to other objects(that includes classes), contain raw data(bytes, integers, characters) and contain functions(multiple lines of re-usable code) to make coding more intuitive and user friendly (supposed to any way). Classes are more meta than functional. This means it is more for the programmer, not the program.

Lastly, I looked up functions being objects. It says that in python they are but don’t even get to worked up about it. Many terms in computer science have different meanings based on how it is coded or the type of philosophy that is behind it. Here is the definition I got from grepper:

A function is a reusable set of statements to perform a task or calculate a value.
Functions can be passed one or more values and can return a value at the end of their execution.

Just use that.

Happy coding.

Thanks for your informational reply

A class is basically an abstraction to mean “memory containing related functions and variables”

say we have a class containing three ints

class RGB:
     red = 0;
     blue = 0;
     green = 0;

You could imagine each of those colors as a house on a street. The class is a blueprint of the street, it says “This street has three houses, the houses look like this”. When you actually build the street - you have an instance of that class, an object(you built something based on the blueprint) The object(street) holds memory addresses(houses). Each house represents a variable or function in our class.
So if you want to use a function or data in a class you have to

  1. Design the street (class aka memory layout)
  2. Build the street (instantiate a class object)
  3. Refer to the houses (use the variables and methods)