Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Beginner's conceptual question about OOP and persistence

This is a very basic question about OOP (I'm using python but really it's a conceptual question so not really language specific). I've looked around but no tutorials or books cover this specific question. If I am unclear I apologise and would be happy to clarify anything I've written.

Let's say I'm creating a simple address book that I want to write to disk using pickle. I have a class called Contact where __init__ takes in some args (firstName and lastName) and I have a menu where you can create contacts, edit them etc. For all examples of OOP I have seen they would do something like...

bob = Contact('Bob', 'Smith') 
jane = Contact('Jane', 'Smith')

...to create new instances of Contact. But these are all defined before runtime. What happens when I want all these instances created on the fly by user input? Do I create new instances for each person? How would I do this from user input? Then just write all the instances to a list and pickle it? Or do you do something like...

firstName, lastName = raw_input("Enter first name: "), raw_input("Enter last name: ")    
contact = Contact(firstName, lastName)

...then just append contact to the list and get new values for the contact instance every time I want to add a user? This is a key concept that I'm not really getting (because I haven't seen it explained really anywhere). All examples I've seen don't do the above but instead create new instances for each thing/person but all of them are pre-defined and not created on the fly. I would be really grateful for someone to explain this concept to me.

like image 764
johnharris85 Avatar asked Jan 01 '11 21:01

johnharris85


People also ask

Which OOP concept solves problem in the implementation level?

Encapsulation solves the problem in the implementation level. 2. Abstraction hides unwanted data and provides relevant data.

What are the OOP concepts with real life examples?

It is a mental component rather than a physical thing. Let's take an example of one of the OOPs concepts with real time examples: If you had a class called “Expensive Cars,” it could contain objects like Mercedes, BMW, Toyota, and so on. The price or speed of these autos could be one of its attributes (data).

How does OOP help in representing real world?

OOP importance: Real-world objects have properties such as car model, car size for car, name, age, occupation for human, etc. and real-world entities or objects can do various things like bikes can move, people can walk or eat, etc. So OOP concept helps the program to be closer to real-world objects.


2 Answers

Your example is exactly how it works.

like image 141
Mariusz Avatar answered Sep 22 '22 04:09

Mariusz


Yes, that's generally how you do it - make arrays of your objects. Or some other kind of collections, depending on your language and/or framework. When creating a new object, you first create it in a temporary variable, and then insert it into your collection.

Sometimes, when you have a LOT of objects, you don't load them all at once from your persisted storage (like a DB or a file). You just load the one (or few) that you need to work with. If you load just one, it might get a special variable. Several will get a collection again.

like image 1
Vilx- Avatar answered Sep 24 '22 04:09

Vilx-