1 8 8

: OOP Python

  1. #1
    .: :.
    Debian 6
    XFCE

    Sep 2009
    1,693
    1,829
    2,964 945

    OOP Python



    . C++ .

    ﻻ " ".

    . ﻻ ﻻ .




    :

    * .
    * def .
    * . .




    ﻻ . . + - . .


    Class

    +,-,, class MyMath :

    PHP:
    #!/usr/bin/python
    # -*- coding: utf-8 -*-

    class MyMath:
        
    pass 
    :
    PHP:
    #!/usr/bin/python
    # -*- coding: utf-8 -*-

    class MyMath(object):
        
    pass 
    class . MyMath.




    () +,-,,. class ( ):

    PHP:
    #!/usr/bin/python
    # -*- coding: utf-8 -*-

    class MyMath(object):

        
    def add(selfxy):
            return 
    y

        def sub
    (selfxy):
            return 
    y

        def mul
    (selfxy):
            return 
    y

        def div
    (selfxy):
            return 

    self class.


    class

    . . :

    PHP:
    #!/usr/bin/python
    # -*- coding: utf-8 -*-

    class MyMath(object):

        
    def add(selfxy):
            return 
    y

        def sub
    (selfxy):
            return 
    y

        def mul
    (selfxy):
            return 
    y

        def div
    (selfxy):
            return 
    y

    mymath  
    MyMath()  #   

    #   
    print mymath.add(12)  # 1 + 2 = 3
    print mymath.sub(53)  # 5 - 3 = 2
    print mymath.mul(25)  # 2 * 5 = 10
    print mymath.div(42)  # 4 / 2 = 2 
    mymath. :
    PHP:
    foo Boo() 
    foo class Boo . class.

    PHP:
    foo Boo()
    koo Boo() 
    class.




    AdvMyMath , $:, , add, sub .. , . AdvMyMath MyMath :

    PHP:
    #!/usr/bin/python
    # -*- coding: utf-8 -*-


    class MyMath(object):

        
    def add(selfxy):
            return 
    y

        def sub
    (selfxy):
            return 
    y

        def mul
    (selfxy):
            return 
    y

        def div
    (selfxy):
            return 
    y


    class AdvMyMath(MyMath):    # <  

        
    def power(selfxy):
            return 
    ** y


    mymath  
    MyMath()

    print 
    mymath.add(12)  # 1 + 2 = 3
    print mymath.sub(53)  # 5 - 3 = 2
    print mymath.mul(25)  # 2 * 5 = 10
    print mymath.div(42)  # 4 / 2 = 2

    mymath2 AdvMyMath()

    print 
    mymath2.add(12)  # 1 + 2 = 3
    print mymath2.sub(53)  # 5 - 3 = 2
    print mymath2.mul(25)  # 2 * 5 = 10
    print mymath2.div(42)  # 4 / 2 = 2
    print mymath2.power(22)  # 2 ^ 2 = 4 <-- NEW!! 
    ﻻ MyMath AdvMyMath :

    PHP:
    class AdvMyMath(MyMath):    # <   
    power AdvMyMath.




    ﻻ div MyMath, 1 2 0 0.5, AdvMyMath , methods overriding :

    PHP:
    #!/usr/bin/python
    # -*- coding: utf-8 -*-

    class MyMath(object):

        
    def add(selfxy):
            return 
    y

        def sub
    (selfxy):
            return 
    y

        def mul
    (selfxy):
            return 
    y

        def div
    (selfxy):
            return 
    y


    class AdvMyMath(MyMath):

        
    # Overriden
        
    def div(selfxy):
            return 
    float(x) / float(y)

        
    def power(selfxy):
            return 
    ** y


    mymath  
    MyMath()
    print 
    mymath.div(12)  # 1 / 2 = 0

    mymath2 AdvMyMath()
    print 
    mymath2.div(12# 1 / 2 = 0.5 



    class , __init__ :

    PHP:
    #!/usr/bin/python
    # -*- coding: utf-8 -*-

    class MyMath(object):

        
    def __init__(selfnum1num2):
            
    self.x  num1
            self
    .y  num2

        def add
    (self):
            return 
    self.self.y

        def sub
    (self):
            return 
    self.self.y

        def mul
    (self):
            return 
    self.self.y

        def div
    (self):
            return 
    self.self.y

    mymath  
    MyMath(21)  #      

    print mymath.add()  # 2 + 1 = 3
    print mymath.sub()  # 2 - 1 = 1
    print mymath.mul()  # 2 * 1 = 2
    print mymath.div()  # 2 / 1 = 2 
    2 1 mymath.

    * , self .
    * , __init__ :
    :
    $ python
    Python 2.6.6 (r266:84292, Dec 27 2010, 00:02:40) 
    [GCC 4.4.5] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>>
    >>> class MyClass(object):
    ...     def __init__(self):
    ...             print "You've called me! :)"
    ... 
    >>> myclass = MyClass()
    :
    You've called me! :)
     >>> 


    __del__ :

    :
    >>> 
    >>> class MyClass(object):
    ...     def __init__(self):
    ...             print "You've called me! :)"
    ...     def __del__(self):
    ...             print "Byebye :("
    ... 
    >>> myclass = MyClass()
    :
    You've called me! :)
     >>> 
    >>> myclass = 2
    Byebye :(
    >>>
    >>> myclass = MyClass()
    You've called me! :)
     >>>
    >>> del myclass
    Byebye :(
    >>> 
    >>> myclass = MyClass()
    You've called me! :)
     >>> exit()
    Byebye :(
    $


    class , .
    * . .
    * self . mymath self mymath.

    :

    PHP:
    #!/usr/bin/python
    # -*- coding: utf-8 -*-

    class MyClass():

        
    def __init__(self):
            
    10

        def pprint
    (self):
            print 
    x

    myclass     
    MyClass()
    myclass.pprint() 
    :

    :
    $ python tot.py 
    Traceback (most recent call last):
      File "tot.py", line 21, in <module>
        myclass.pprint()
      File "tot.py", line 18, in pprint
        print x
    NameError: global name 'x' is not defined
    $
    x , ﻻ . self :

    PHP:
    #!/usr/bin/python
    # -*- coding: utf-8 -*-

    class MyClass():

        
    def __init__(self):
            
    self.10

        def pprint
    (self):
            print 
    self.x

    myclass     
    MyClass()
    myclass.pprint() 
    :
    $ python tot.py 
    10
    $



    ( ) :

    PHP:
    #!/usr/bin/python
    # -*- coding: utf-8 -*-

    class MyMath(object):

        
    def __init__(selfnum1num2):
            
    self.x  num1
            self
    .y  num2

        def add
    (self):
            return 
    self.self.y

        def sub
    (self):
            return 
    self.self.y

        def mul
    (self):
            return 
    self.self.y

        def div
    (self):
            return 
    self.self.y

    mymath  
    MyMath(21)

    print 
    mymath.add()  # 2 + 1 = 3

    mymath.x    # < !
    mymath.y    # < !

    print mymath.add()  # 4 + 1 = 5 
    , ﻻ __ ( )
    PHP:
    #!/usr/bin/python
    # -*- coding: utf-8 -*-

    class MyMath(object):

        
    def __init__(selfnum1num2):
            
    self.__x  num1
            self
    .__y  num2

        def add
    (self):
            return 
    self.__x self.__y

        def sub
    (self):
            return 
    self.__x self.__y

        def mul
    (self):
            return 
    self.__x self.__y

        def div
    (self):
            return 
    self.__x self.__y

    mymath  
    MyMath(21)

    print 
    mymath.add()  # 2 + 1 = 3

    mymath.__x   232
    mymath
    .__y   112

    print mymath.add()  # 2 + 1 = 3 ﻻ  



    __ , class ﻻ class class , float :

    PHP:
    #!/usr/bin/python
    # -*- coding: utf-8 -*-

    class MyMath(object):

        
    def __init__(selfnum1num2):
            
    self.__x  num1
            self
    .__y  num2

        def add
    (self):
            return 
    self.__x self.__y

        def sub
    (self):
            return 
    self.__x self.__y

        def mul
    (self):
            return 
    self.__x self.__y

        def div
    (self):
            return 
    self.__to_float(self.__x) / self.__to_float(self.__y)
        
        
    # Private method
        
    def __to_float(selfnumber):
            return 
    float(number)

    mymath  MyMath(21)

    print 
    mymath.div() 
    class __to_float float. :
    PHP:
    #!/usr/bin/python
    # -*- coding: utf-8 -*-

    class MyMath(object):

        
    def __init__(selfnum1num2):
            
    self.__x  num1
            self
    .__y  num2

        def add
    (self):
            return 
    self.__x self.__y

        def sub
    (self):
            return 
    self.__x self.__y

        def mul
    (self):
            return 
    self.__x self.__y

        def div
    (self):
            return 
    self.__to_float(self.__x) / self.__to_float(self.__y)
            
        
    def __to_float(selfnumber):
            return 
    float(number)

    mymath  MyMath(21)

    print 
    mymath.div()

    # Error!
    print mymath.__to_float(3
    :

    :
    $ python tot.py
    2.0
    Traceback (most recent call last):
      File "tot.py", line 26, in <module>
        print mymath.__to_float(3)
    AttributeError: MyMath instance has no attribute '__to_float'
    $
    . :


    :

    . ,

    PHP:
    class Boo(object):

        
    def __init__(self):
            
    self.x      23
            self
    .__y    "Hi"

        
    def poo(self):
            
    pass

        def __roo
    (self):
            
    pass

    foo 
    Boo()
    foo.poo() 
    class Boo
    method poo
    private method __roo
    attribute x
    private attribute __y
    object foo

    :

    PHP:
    #!/usr/bin/python
    # -*- coding: utf-8 -*-

    def MyMath_add(xy):
        return 
    y

    def MyMath_sub
    (xy):
        return 
    y

    def MyMath_mul
    (xy):
        return 
    y

    def MyMath_div
    (xy):
        return 
    y

    print MyMath_add(32
    .

    PHP:
    #!/usr/bin/python
    # -*- coding: utf-8 -*-

    class MyMath(object):

        
    def add(selfxy):
            return 
    y

        def sub
    (selfxy):
            return 
    y

        def mul
    (selfxy):
            return 
    y

        def div
    (selfxy):
            return 
    y

    mymath  
    MyMath()
    print 
    mymath.add(32
    .

    .


    ; 28-04-2012 12:57 AM


  2. #2
    .:: ::.
    Mint 13
    GNOME

    May 2008
    1,381
    1,240
    2,064 709









    ###########

    " "

    -
    --------
    LightBlog


  3. #3
    Fedora 16
    GNOME3

    Jan 2010
    24
    9
    5 3

    oop !!!!
    ﻻ ﻻ !!

  4. 3 G705T :


  5. #4
    Ubuntu 10.10
    GNOME

    Feb 2009
    145
    86
    6 6


  6. #5
    GNOME

    Jan 2014
    2
    0
    0 0


  7. #6
    Banned
    Unity

    Mar 2015
    1
    0
    0 0

    ; 23-03-2017 11:47 AM

  8. #7
    Arch
    GNOME

    Oct 2011
    11
    3
    6 5

    self ==this

  9. #8
    Kali Linux
    Window Maker

    Mar 2017
    2
    3
    0 0


: 1 (0 1 )

  1. [] vedics ( )
    .: :.
    : 6
    : 04-08-2012, 04:59 PM
  2. []
    ardous5 .: :.
    : 5
    : 22-09-2010, 08:51 PM
  3. []
    abdulmughni9 .: :.
    : 6
    : 24-04-2010, 07:48 AM
  4. []
    .: :.
    : 12
    : 10-05-2009, 03:08 AM
  5. [] glade python
    dr_med_ra .: :.
    : 5
    : 05-07-2008, 10:07 PM