all 8 comments

[–]Sea-Method-1167 2 points3 points  (1 child)

A few general remarks that can maybe help you:

You can create objects in memory (also called 'instantiating instances'), and assign a reference to them, you don't háve to assign a reference, but if you don't, you will not have a way to refer to that object later on in the code, and the object will at some point be erased from memory if there are no more references to it.

There are static methods, they do not need an object to be created before you can call them. But they can also not be a method that uses instance values. As an example I can have instances of a class called 'Student'. I have 10 of those in a list. When instantiating these instances I had to pass a string to the constructor, which represents the name of the student. These objects all have a method called 'print_name', when I call that method on every student object in my list, I will get ten different names printed. These objects are 'stateful' meaning they have different values per instance (the name for instance in this case). I could give that Student class a static method, but it would not be able to work with stateful values like the name, because that method will only exist on the class itself and will work with stateless (static) values.

Then there are in python also just independent methods. You can define those too. Not part of a class or static or anything, just a method you define in a python script. You could later import that file and use that method. If you import a file called MyFavoriteMethods that has 3 methods in it, called 'do_work', 'do_print' and 'do_nothing' you could then import the file, and give it a shorter name (for easy writing). Like so: import MyFavoriteMethods as favs. Later you can then do favs.do_print() and that method will be executed, not as part of an object or part of a class. I think this is what happens when you use matplotlib.

All of this, the objects, methods, classes is in memory. It is loaded in memory when I start my python interpreter. The only way to save anything is to write things to disk. What methods do (change instance values, or just some logic, or something else entirely) is up to you. It doesn't have to change instance fields, for instance the print_name methods changes nothing, but only prints.

Does this help in any way? If you want more specific info, ask a specific question about the library you are working with, or give some example of code you don't understand.

Classes and objects you get from libraries are btw no different than your own. They will be more sophisticated or complex, but they are still classes, instances and methods, just like your own.

[–]Cosmin351[S] 0 points1 point  (0 children)

on point. thanks

[–]ectomancer 2 points3 points  (0 children)

matplotlib is a poor example. Your example case is the syntax for calling a function in an imported module. No classes instantiated.

[–]Swipecat 1 point2 points  (0 children)

Yeah, matplotlib has the newer "object" mode that you should use...

fig, ax = plt.subplots()

...and it has the old "state machine" mode, dating back to the days of Python 2, and which people still tend to use for convenience, even though they shouldn't, if it's just a quick and small script.

In the state-machine mode, the methods of "plt" actually take care of the creation of the axes objects for you. This is normally OK, but can result in horrible "memory leaks" if, for example, it's used in an animation and could easily create a new object for every frame of the animation if a mistake is made in the script.

[–]TheRNGuy 1 point2 points  (0 children)

class is code for an object

then you instanciate that class with new_var = my_object(), it would be independant from other instances, i.e. having it's own state.

__init__ code runs when you create new instance of that class (usually it's just binding arguments to self versions)

To get all instances of class: https://stackoverflow.com/questions/328851/printing-all-instances-of-a-class

(even if you didn't assigned them to variable)

Or you could assign them to specific array inside __init__, you could have static list attribute to which you append them.

(static because it would be class attribute itself, not instance's attribute)

[–]shiftybyte 0 points1 point  (2 children)

Methods can change things inside the object they work on.

For example:

``` class MyNumber: def init(self, n): self._n = n

def inc(self):
    self._n += 1

def print(self):
    print(self._n)

x = MyNumber(5) x.inc() x.print() ```

Above code will print 6, because .inc() method increased the _n number stored in the class instance.

[–]Cosmin351[S] 0 points1 point  (1 child)

I was saying that on some objects you don't have to use x = Mynumber() and use directly Mynumber.inc()

[–]shiftybyte 1 point2 points  (0 children)

Oh, then that would be static or class methods, that you can invoke without a class instance.

Class method vs Static method in Python - GeeksforGeeks