Hello

I can't understand OOP in python
I am looking to good tutorial , i am looking to good tutorial , about OOP in python from zero
Thank you

Recommended Answers

All 15 Replies

What part of OOP don't you understand?

class Student: 
    def __init__(self, name, age, n1, n2): 
        self.name=str(name) 
        self.age=str(age) 
        self.n1=int(n1) 
        self.n2=int(n2) 
         
    def moy(self): 
        m=(self.n1+self.n2)/2 
        print m 

s=Student('Jhon', '18',15, 19) 
s.moy()

I don't understand the red

It is to initialize things your going to need in the class. It is also where you receive the parameters the class needs. Anything you see that looks like "self.name" can be used by the methods inside the class.

Sorry
Can you explain, by my code previous to be more clearly

Sorry
Can you explain, by my code previous to be more clearly

Imagine a student's record like a box containing 4 items: name, age and the 2 marks n1 and n2. The role of the __init__ method is to fill the box initially. It is called when you create the student record with s=Student('John', '18', 15, 19) . The box (or Student instance) itself is called 'self' in the class and the items (attributes) are called self.name, self.age, etc. The role of other methods like moy() is to manipulate the values contained in the box to extract information or modify these values.

OK, to be at picture
I wrote simple program :

class Letters():
    def __init__(self,letter1,letter2):
        self.letter1=str(letter1)
        self.letter2=str(letter2)

    def L1(self):
        self.letter1=str(raw_input('add your letters'))
        print 'your letters is :',self.letter1

    def L2(self):
        print 'big :',str.upper(self.letter1)
        print 'small :',str.lower(self.letter1)

l=Letters('letter1','letter1')
l.L1()
l.L2()

and in this part

l=Letters('letter1','letter1')

I don't understand it.

thank you very much.

l=Letters('letter1','letter1')

It's strange that you don't understand code that you wrote yourself. This line creates a new Letters instance (a new 'box' with the terminology of the previous post). It calls the __init__() methods with 2 strings as arguments, which are used to fill the box. Try with l=Letters("hello", "world").

well, (attributes) what is Interest me in my program ?
and what is the right method to used ?

Member Avatar for Nirvin M

I think you should get a clear picture on what OOP is. First before learning Python go through a good C++, Java or C# tutorial and try to learn Python. If are good at C++ or other OOP language, then go through this python tutorial -> http://www.tutorialspoint.com/python/index.htm

I think you should get a clear picture on what OOP is. First before learning Python go through a good C++, Java or C# tutorial and try to learn Python. If are good at C++ or other OOP language, then go through this python tutorial -> http://www.tutorialspoint.com/python/index.htm

I think python is a much more friendly language to learn object oriented programming than C++, java and C#. If your goal is to program in python, don't learn C++ first, it would take you months or more to learn C++. I agree that it would teach you OOP, but with many C++ specific issues that have little to do with OOP.

The only real way to learn OOP is to write and run object oriented programs.

Seems like the big barrier here is that the poster's primary language must not be English. I tried searching for "moy", but I didn't get any promising results. Perhaps it is an abbreviation or truncation?

Seems like the big barrier here is that the poster's primary language must not be English. I tried searching for "moy", but I didn't get any promising results. Perhaps it is an abbreviation or truncation?

It could be an abbreviation of the french 'moyenne' which means average.

Let me try to explain object-oriented programming.

Object-oriented programming is programming with objects.

Objects are data structures consisting of fields of data (attributes) and functions that interact with that data (methods).

Objects are instances of classes (types). Classes represent the data structure. When you make a new class, you are "declaring" a class.

In Python, classes possess special methods for certain functions. Their names are usually between double underscores. "__init__" is a special method to initialize a new object and its data. The call to __init__ is usually implicit. Normal methods have an implied first argument (usually called "self") that reflect back to the object. Methods can access an instance's data using "self".

An example:

class Person:

    #Special method. Each person we make has a name.
    def __init__(self, name):
        self.name = name

    #Method accesses the instance's name to print it.
    def introduce(self):
        print(name, 'says hello.')

#__init__ called implicitly
#sets this person's (and only this person's) name
x = Person('John')
#a different person
y = Person('Jane')
#printing their names
x.introduce()
y.introduce()

Erm... Didn't notuce until just now, but the line "print(name, 'says hello.')" should be "print(self.name, 'says hello.')".

Sorry.

Thank you very much for everyone

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.