r/learnpython • ¯\_(ツ)_/¯ • Feb 13 '21

HOW HAVE I BEEN SO BLIND!!!!

IT IS ALL OBJECT....

EVERYTHING IS OBEJCT!!!!

EVERYTHING!!!!!

OH MY GOD!!!!

IT'S ALL OBJECT!!!!!!

AHHHHH!!!!

1.3k Upvotes

200 comments sorted by

View all comments

Show parent comments

17

u/Nixellion Feb 13 '21

Everything in Python is an object as in a class/instance of a class, and can be treated the same way.

8

u/SuperSaiyanIR Feb 13 '21

How would that be more useful when compared to java or other programming languages that are not object oriented? Why is this a groundbreaking realization? I’m still learning these languages so I’m don’t get why it might be so impactful.

15

u/ianepperson Feb 13 '21

Take something really really simple: like the number 5.

It's an object. With properties and methods.

5.bit_length()

tells you how many bits are required to store the number 5.

7

u/TangibleLight Feb 13 '21

Minor nitpick: that gives a syntax error because Python tries to interpret 5. as a float. You need (5).bit_length().

23

u/[deleted] Feb 13 '21

Just gonna point out that Java is extremely object-oriented.

11

u/[deleted] Feb 13 '21

It really helps you organize things. When you create a class, as you do in Java and python (and all object oriented languages), you give the class variables and methods/functions associated with it. Then in your code outside the class, in your main method, you create instances of the class. The instance is called an object.

For example, pretend you are trying to create a model of a bank. The bank has lots and lots of bank accounts, so you write a class that represents a generic bank account. The class contains the variables “owners first name,” “owners last name,” and “balance.” The class also contains the functions “withdraw,” “deposit,” and “close account.”

In your main method, you create a bank account by saying account1 = bankAccount(John, Smith, 1000). When John wants to deposit money, you say account1.deposit(500). And then when you want to know John’s balance, you can access it by typing account1.balance. This would return the value 1500. And then to close the account, you type account1.closeAccount().

This type of coding has a bunch of benefits (and drawbacks). It is really helpful when you have thousands of customers and bank accounts to keep track of. It’s also helpful because a bank might have lots of other objects associated with it: customers, employees, cash vaults, loans, investment portfolios etc. Each of these things would have their own set of functions associated with them, so for example you wouldn’t ever use a “withdraw” function on an object representing a loan contract. By creating classes, you make it clear that the withdraw function can only be used on the bankAccount object. This makes your code much easier to read and maintain for the people who take over the role of maintaining that code in the future.

As you continue learning about OOP, you’ll start to learn about the other benefits like inheritance and abstraction. Inheritance is when you have child and parent classes, where the children inherit the properties of the parent class. For example, you can make an “animal” class with a function like “make noise.” Then you make the child class “dog” with the function “bark.” This has some benefits with organization, code readability, and even write-ability. Abstraction let’s you call functions that you don’t need to actually understand, cause someone else wrote the function and documented how it works.

3

u/benign_said Feb 13 '21

This was well written and helpful, thanks!

2

u/[deleted] Feb 13 '21

You're welcome.

3

u/SuperSaiyanIR Feb 13 '21

This really helps explain a lot. I will be saving it for future references when I understand the terms better lol.

0

u/[deleted] Feb 13 '21

[deleted]

2

u/[deleted] Feb 14 '21

I mean there isn’t really an answer. Python and C++ and similar languages are technically OOP but you can avoid anything with classes and objects when using them. It’s a weird technicality without a real answer. I’d say it’s only OOP if you use classes and objects, but i’m sure someone will disagree

1

u/notParticularlyAnony Feb 14 '21

No and that's ok it's python

8

u/Shploosh Feb 13 '21

Java is an object oriented language.

OOP essentially give a few advantages, for example abstraction and modularity. Abstraction allows users to use code without knowing exactly how it is implemented, as well as protecting existing, working code from being tampered with. Modularity prevents a user from having to rewrite the same snippet of code over and over, which can lead to more sources of bugs.

6

u/max123246 Feb 13 '21 edited Jan 30 '26

This post was mass deleted and anonymized with Redact

middle disarm follow plant cake squeal thumb retire bells pet

2

u/Shploosh Feb 13 '21

Fair enough. To be honest I don't have a lot of experience with non OOP languages, so I probably underestimated their capabilities.

You are absolutely correct on the power of classes and objects stemming from the tying together of data and the operations on said data. I suppose that abstraction and modularity are inherent in OOP languages, though not exclusive to them.

4

u/max123246 Feb 13 '21 edited Jan 30 '26

This post was mass deleted and anonymized with Redact

vase advise safe coherent cagey fine rainstorm strong voracious deserve

1

u/Wallterprof Feb 13 '21

Python does not support strong encapsulation and java have primitive types. Non of these languages considered strongly or pure object oriented. Object-oriented approach only helps you to write more logical and cleaner code.

1

u/Raedukol Feb 13 '21

What are the practical consequences out of this?

1

u/Nixellion Feb 13 '21

Check other replies to this post, there are some good explanations there. On phone, can't reply in detail. Or keep learning and it will dawn on you :D