+ 1

What's object in oop?

25th Jan 2023, 4:59 AM
Santiago Angel mancera
Santiago Angel mancera - avatar
22 Answers
+ 6
(1) Excuse my explanation cos it may be difficult to comprehend.. Let me start by explaining what the primitive variable does in python. you can create info about people like so name1 = "Satoshi Nakamoto" age1 = 53 name2 = "Selena Gomez" age2 = 30 now imagine you want to create infomations for about 20 people, things will start getting complex. you'll probably run out of names idea and your code will get messed up. This is where objects in oop comes in. Allow me to explain what happened when we create a variable in the example given above. 1. Memory is allocated: The computer sets aside a specific amount of memory for the variable based on its data type. For example, an integer variable takes up more memory than a boolean variable. 2. The variable is given a name: The variable is given a name that you, the programmer, assigns. This name is used to reference the variable in the rest of your code. 3. The variable is assigned a value: You can assign an initial value to the variable when you create it or you can assign it later in your code. 4. The variable is stored in a specific memory location: The computer assigns a specific memory address to the variable, which is used to reference the variable in the memory. This memory location is unique and specific to the variable. 5. The variable is ready to use: Once the variable is created and assigned a value, you can use it in your program. You can use the variable to store and manipulate data, perform calculations, and more. Now, in oop you can create a class like this class User: def __init__(self, name, age): self.name = name self.age = age and an object like this satoshi = User("Satoshi Nakomoto", 53) after creating the instance of an object, member variables like (self.name, self.age) also works like the regular name1, age1 variables i created but there is a difference, their addressed have been indexed by the address of the instance they are from. still difficult to comprehend? Let me explain what that means
25th Jan 2023, 6:34 AM
Mirielle
Mirielle - avatar
+ 4
Technically, everything in Python is an object - it’s an object oriented programming language. A plain old variable in Python is still an object (well a pointer, to the object which contains all the metadata and methods). Creating your own class User is no different than using int…you are just using two different types of objects! Both are classes, constructors are called etc. Sure it’s sensible and more convenient grouping data together, but I don’t see how that would help with running out of names - you still have to instantiate an object with a name. Here’s something to try, in your Python terminal type ‘help(int)’. I’ll copy a small section below, you will see its a class just like the user defined class. Help on class int in module builtins: class int(object) | int([x]) -> integer | int(x, base=10) -> integer …etc So the answer to your question, what is an object in Python: everything.
25th Jan 2023, 4:30 PM
DavX
DavX - avatar
+ 3
Hi Santiago Angel mancera apart from the many answers provided here. I found an extra good source to further your education. This site explains what OOP means, whats an object and how to create them. It provides many logical and easy to follow code examples. - Happy Coding! https://realpython.com/python3-object-oriented-programming/
27th Jan 2023, 7:20 PM
Chris Coder
Chris Coder - avatar
+ 2
python Hope it's helpful to you
25th Jan 2023, 5:41 AM
Sakshi
Sakshi - avatar
+ 2
No primitives in Python! Everything's an object. Python is not Java. Even a literal is an object, try: print(type(“string”)) A string literal is a str object.
25th Jan 2023, 8:03 PM
DavX
DavX - avatar
+ 2
…suggest you go an do a little research before telling me I don’t know what OOP is. Millions of places online where you will find that you are clearly wrong. I’ve obviously rubbed you up the wrong way, but just like your “pass by pointer” you are wrong. Just accept it. I’ve shown you through the builtins help but if your still not convinced then checkout intobject.c from the Python source code. Even ask your Nvidia buddies, standards must be getting low around there…
26th Jan 2023, 4:58 AM
DavX
DavX - avatar
+ 2
Ahahaha, right. Again you are making assumptions, I didn’t suggest you learn from bloggers - I suggested you learn from python source code. I haven’t been trolling either - You insulted me first so deal with it! That fact is you are incorrect, show me evidence otherwise and i will concede.
26th Jan 2023, 7:29 AM
DavX
DavX - avatar
+ 2
Your going off again, I never stated Python was pure oop, where have I said pure? I’m talking about the lack of primitives in python, that int is an object. That everything is an object in Python. …you can keep your Kotlin example, this question is related to Python!
26th Jan 2023, 7:58 AM
DavX
DavX - avatar
+ 2
Jay Matthews nice demonstration
26th Jan 2023, 9:15 AM
Mirielle
Mirielle - avatar
+ 2
Jay, Thanks for your input, aside from the fact that magic methods shouldn't be invoked directly... The error from your calling is wrong, try this which works fine: print((5).__add__(3)) or INTCON = 3 print(INTCON.__add__(4)) or for a boolean print((True).__add__(False)) To expant on this, you could also add a space so that the 5 isn’t parsed as a float: print(5 .__add__(3))
26th Jan 2023, 9:52 AM
DavX
DavX - avatar
+ 1
(2) The address for each variables could be variables address --------------------------- name1 1 age1 3 name2 2 age2 6 satoshi 8 satoshi.name 9 satoshi.age 10 satoshi shows the offset memory for it's other variables, you can simply get satoshi.name by typing (satoshi.address + 1) and satoshi.age (satoshi.address + 2) OOP take cares of these under the hood.. The grouped memory addresses for satoshi makes it an object because it can be thought of as real-world objects. Each object contains data and behavior, and can interact with other objects. The main idea behind OOP is to use objects to model real-world objects and situations, so that the program can be more intuitive, easier to understand, and easier to change.
25th Jan 2023, 6:42 AM
Mirielle
Mirielle - avatar
+ 1
Básicamente la OOP permite a los programadores escribir software, de forma que esté organizado en la misma manera que el problema que trata de modelizar.
26th Jan 2023, 6:09 AM
Andrés Fernando Pérez Machado
Andrés Fernando Pérez Machado - avatar
+ 1
Object is an variable of class type
26th Jan 2023, 10:00 PM
Yash Chaudhari
Yash Chaudhari - avatar
+ 1
Mirielle technically age1, age2, will be stored in same memory since its python
26th Jan 2023, 10:31 PM
Ion Kare
Ion Kare - avatar
+ 1
Everything is an object as stated above , i played around with python and they abstracted this by using metaclasses so it wont show
26th Jan 2023, 10:36 PM
Ion Kare
Ion Kare - avatar
+ 1
He want to know what is oop as you said its a way to group releated things together, also distinguishing truth values on other ,
26th Jan 2023, 10:50 PM
Ion Kare
Ion Kare - avatar
+ 1
Just think of object as obj of a class and when you declare a class, you have to create objects to access the data from that class. Class A { private: Int age; public: void setAge(int Age){age=Age;} Int getAge(){return age;} }; Int main(){ A objA1; < creating an object A objA2; < creating an object objA1.setAge(22); objA1.getAge(); objA2.setAge(32); objA2.getAge(); objA3.setAge(54); objA3.getAge(); As you can see you are creating different objects for that class. }
27th Jan 2023, 3:57 AM
Andy Rich
Andy Rich - avatar
0
Originally, the definition for an object is the smallest accessible part In a memory...usually the bytes of a character . So I agree if you say everything Is an object in python as long as they have an accessible byte space in memory. However, in a grammatical context... There is a difference between object in oop and object as a general context. Object in oop means something else ... Related but not the same. python being an object oriented language however does not means it's fully object oriented language, in the sense that it has no support for encapsulation plus primitive types and literals like 1, 10 etc which aren't object.. take for example the Kotlin language. Kotlin is purely object oriented language while python isn't
25th Jan 2023, 7:43 PM
Mirielle
Mirielle - avatar
0
Values like 1 has no member variables or functions. If you actually know what oop is all about, you'll realised python is not fully oop It doesn't even support encapsulation lol, which I believe is one of the key pillars why oop was created
26th Jan 2023, 3:47 AM
Mirielle
Mirielle - avatar
0
DavX I'm certified with honors and don't need some article bloggers to learn from... I'm a source myself. However, it's okay for you to assume whatever you feel like, also it's normal to argue and learn through discussion. I may be forced to block your profile (which I mostly do) if you think trolling and or getting my job involve in some python argument is cool
26th Jan 2023, 7:09 AM
Mirielle
Mirielle - avatar