Followers 0

# OOP is so confusing[wrong question]

## 9 posts in this topic

Hi guys i want to know the process of object orientated programming(OOP) i tried my best to understand what is exactly the process but i have no idea what is it.I have a code like this:



class Song(object):

def __init__(self, lyrics):
self.lyrics = lyrics

def sing_me_a_song(self):
for line in self.lyrics:
print line

happy_bday = Song(["Happy birthday to you",
"I don't want to get sued",
"So I'll stop right there"])

bulls_on_parade = Song(["They rally around the family",
"With pockets full of shell"])

happy_bday.sing_me_a_song()



And i don't understand what is "for" and "in" statement it is really confusing me. and i don't understand what :



self.lyrics = lyrics


and



for line in self.lyric:
print line


is do for .Sorry for asking to much

Edited by Kurai Tsubasa
0

##### Share on other sites

Where did you find this code? Are you reading a book or following a tutorial online? If so, are you doing the excersizes after each chapter?

A good set of introductory material should be explaining each of these concepts on their own.

0

##### Share on other sites

Hi guys i want to know the process of object orientated programming(OOP) i tried my best to understand what is exactly the process but i have no idea what is it.I have a code like this:



class Song(object):

def __init__(self, lyrics):
self.lyrics = lyrics

def sing_me_a_song(self):
for line in self.lyrics:
print line

happy_bday = Song(["Happy birthday to you",
"I don't want to get sued",
"So I'll stop right there"])

bulls_on_parade = Song(["They rally around the family",
"With pockets full of shell"])

happy_bday.sing_me_a_song()



And i don't understand what is "for" and "in" statement it is really confusing me. and i don't understand what :



self.lyrics = lyrics


and



for line in self.lyric:
print line


is do for .Sorry for asking to much

This problem here is nothing really to do with object oriented programming. Keywords like 'for' and 'in' are fundamental to python (and a lot of other programming languages -- object oriented or not) so I would suggest that you take I look a 'getting started' tutorial for python.

-Josh

The question has nothing to do with object orientation. That is simply Python's for loop. I would strongly suggest taking a few steps back, you should learn more about the fundamentals of your chosen language before you try learning about more advanced concepts.

Where did you find this code? Are you reading a book or following a tutorial online? If so, are you doing the excersizes after each chapter?

A good set of introductory material should be explaining each of these concepts on their own.

oh,sorry i think i misunderstanding this,i was thinking this is OOP.Sorry ^^a

0

##### Share on other sites

The code involves OOP, sure. But the root cause is that you seem to be trying to understand a (relatively) complex example while being new to the language and not sufficiently exposed to the various constructs.

Yeah i still beginner and i think i'm trying to understand that i haven't ready yet.i guess i will try to find some free books that is easy to learn for beginner^^

Edited by Kurai Tsubasa
0

##### Share on other sites

Attempting ton understand some of the more complex constructs in a language before learning the basics can be quite a bit confusing.

"for" is a loop statement

"in" is used to iterate through a list, dictionary, or array

"self" is a class variable construct - The syntax is a lot like C++ and is confusing to learn at first

I would strongly recommend reading How to Think Like a Computer Scientist: Java or How to Think Like a Computer Scientist: Python and doing the exercises at the end of each chapter.

The Ebooks are free.

Edited by Shippou
2

##### Share on other sites

I had the hardest time understanding Object Oriented Programming. I tried to collect my thoughts and make a tutorial to explain it (I understand it now). It might help.

http://forum.maratis3d.com/viewtopic.php?id=839

The langauge I used is LUA.

If you need me to clarify more, I can.

Edited by Tutorial Doctor
0

##### Share on other sites

To understand OOP (Object Oriented Programming), it's likely best to understand what an Object is when referring to programming.

Consider the following:

Scenario #1

You are a car mechanic.

A car is delivered to you, and it requires a tune up.

You find that several nuts and bolts are lose on examination.

You search for and acquire an adjustable wrench.

Using the adjustable wrench, you tighten the nuts and bolts.

Since there are no other tasks associated with this tune up, the tuneup is completed.

Scenario #2

A second car comes in.

The car requires a replacement radiator.

On examination of the radiator, you determine that an array of tools are required, including an adjustable wrench.

You search for and acquire an adjustable wrench.

You use the adjustable wrench to loosen bolts.

You use other tools to further allow the removal and replacement of the radiator.

You use a combination of tools to again mount the radiator.

You again use the adjustable wrench to tighten the bolts.

The result of using the adjustable wrench returns a completed task (as with the other tools).

Since there are no other tasks associated with this radiator replacement, the replacement is completed.

An object seen in the two examples above would be the adjustable wrench

In programming, a object is essentially a class that gets instantiated, meets a criteria, and can be reused. For proper object creation, it must meet these four requirements:

It must support polymorphism.

It should support overriding (for consistency).

It should support encapsulation (for security).

Much like the adjustable wrench in the above example, the adjustable wrench was performed on both a tune up and a radiator replacement. It essentially was used to remove and tighten bolts and nuts in two different scenarios.  The nuts and bolts between the two cars were different, but were classified as the same type of property of which an adjustable wrench be used with. Likewise, two different sets of code may require the use of a class for different reasons, but pass a similar type of peropty and require a similar return value. This is essentially polymorphism, because the same object was used for the same reason with a different set or properties in two different scenarios (you had to adjust the wrench size for different size nuts/bolts two separate jobs, but got the job done with one adjustable wrench).

Polymorphism

The biggest challenge in OOP is thinking outside of primitives. As novice programmers, we tend to think int, float, double... and the associated values to such. But OOP is both similar and different. An object is a container of values/properties. The easiest way to truly understand an object is to understand a structure in C. You have an entity, and that entity has properties. When you use that entity, you can call one property, a set of properties, or all properties stored. If you want to get more complicated, you can stored objects within objects, and would have to iterate through the first object to access the nested objects, for the values stored in the desired object. Tricky, huh?   To further understand this, I would highly recommend studying data structures.

Overload is hard to explain without confusing people.  I would recommend researching it. In simplest definition, overloading is when you call the same function/method of a class, pass a different set of parameters. The parameters define which of the identically named function/methods you are calling.

Overriding

Overriding is when you are quite literately calling a method of identical name and parameter, but of a child class, which invokes the use of that method over the identical method of a super class.  A clean example of this is when you define a default constructor which is required in instantiating a class (for Java and C#). Another way to look at this is saying you have two classes, class 1, and class 2. You have a function/method called public int GetMe(int i) in both. To get to class 2, you must first instantiate class 1, then using that object, instantiate class 2. But instead of using GetMe(int i) in class 1, you use it in Class 2. You are overriding the GetMe(int i) method of class 1 with the GetMe(int i) method of class 2.

Encapsulation

Encapsulation is when you use access modifiers like private, protected, etc to hide variables from super classes, but give access to change such values through methods like setters/getters.

Again, if you truly want to understand OOP, study polymorphism, overloading, overriding and encapsulation. I would also advise studying further into containers and data structures, and learn how to use iterators. You'll never ask this question again if you know these key aspects. Best of luck to you.

Edited by Subtle_Wonders
1

## Create an account

Register a new account

Followers 0

• 12
• 28
• 14
• 11
• 34