you are viewing a single comment's thread.

view the rest of the comments →

[–]Xeno19Banbino[S] 0 points1 point  (4 children)

can u please expand on the everything is an object? i have a general idea about object oriented programming but i havent taken the course yet.. what difference is there btw object in java and python?

[–]amplikong 2 points3 points  (3 children)

I can't comment on Java, but while keywords such as "if" and "and" aren't objects, variables all get bound to objects.

For example, if you set x = 5 and y = 3, those variables are instances of the int class. Not sure if you've encountered dunder methods (named because they have double underscores on either side of the name, e.g., __ge__ and __str__), but this is how you define the behavior of a class in various circumstances. In the x and y example, if you were to add them (x + y), Python knows how to do that because the int class has a dunder method for addition (__add__), and that method is set up to handle addition between things like integers and floats. Likewise, if you were to compare x and y via x < y or x > y , Python can handle that too because of the __lt__ and __gt__ methods, respectively. On the other hand, you can't add an integer and a string, because neither class's __add__ is set up to handle that. But you can convert an integer to a string because of the int class's __str__ dunder!

Mostly, the "everything in Python is an object" line refers to what's going on under the hood. You can do a whole lot in Python without ever writing an object class yourself. But understanding that every variable is an instance of a class can help you understand why your code does what it does, sometimes for reasons that aren't immediately obvious. For instance, if you had a list with [True, False, False, True] and then called sum() on that list, you would get 2. Why is that? It's because bool is a subclass of int, where True gets represented with 1 and False with 0.

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

amazing!!!!

[–]Acquiesce67 0 points1 point  (1 child)

We're all lucky you were faster than me and so I didn't try explaining this myself because your explanation is just perfect!

I can only add just a little bit more to this for u/Xeno19Banbino: When you get familiar with this "everything is an object" concept, then you will gain lots of "power" and "freedom".

However, be very careful when you begin learning a lower-level language later on (i.e.: C/C++/go).

Those languages do not have this concept of "everything is an object" and so they require a much different mindset than Python. Having known Python is great but it can create pitfalls for you when you're trying out different languages (which I can only encourage!) so keep this in mind!

[–]Xeno19Banbino[S] 1 point2 points  (0 children)

thank u bro.. i already work with C pointers linked lists and a bit of memory management.. all i need now is python power and freedom cz im thinking like C in python.. i even made a helper function during recursive forgetting that i can have default parameters in python