Sahid Ferdjaoui Blog - Funraill Foundation Bienvenue, Log in - thème 2007 RC1

Powered by OpenSource Softwares

Outil de déboggage colaborative
reduire

Polymorphisme sous Python

le 17 mars 2007 par sahid

Introduction au polymorphisme sous Python

Presentation de Python

Python est un langage de programmation de haut niveau, orienté objet, multiplateforme, et Opensource (distribué sous GPL si je ne me trompe pas).

Python est un langage de programmation trop mal connu, et considéré comme un langage trop banal et simple pour etre appris correctement. Pour donner mon avis personnel je dirai que ca vient principalement du coté “langage de scrypt”, PHP et Javascrypt on connu les meme problemes.

Comme je le disais Python est portable, et un logiciel sous python peut tres facilement etre porté. Sa syntaxe permet de mettre en place des applications complexes avec un minimun d’efforts par rapport a un programme en C. Pour finir Python est orienté-objet, et tout dans python est objet…

Avant Prorpos

Bon cette petite introduction ne sera sans doute pas utile au debutant Python, mais ayant été confronté au probleme du polymorphisme j’ai pensé que ça pourrait etre intéressant d’en faire profiter vous mes lecteurs favoris (;, au cas ou un jour vous vous retrouviez confronté ce probleme sous python.

Polymorphisme sous Python

Pour commencer on va mettre en scene 3 classes A, B et C, elles comporteront toutes la methode printItSelf () et retourneront le nom de la classe auxquelles elles appartiennent.

  1. class A:
  2.     """ Classe A """
  3.     printItSelf (self):
  4.         print "J’appartiens  [A]";
  5.  
  6. class B:
  7.     """ Classe B """
  8.     printItSelf (self):
  9.         print "J’appartiens  [B]";
  10.  
  11. class C (A, B):
  12.     """ Classe C, herite de A et B """
  13.     printItSelf (self):
  14.         print "J’appartiens  [C]";

Nous allons maintenant voir comment appeler la methode printItSelf de A et B a partir de notre instance de C

  1. C obj # Instance de C
  2.  
  3. A.printItSelf (obj)
  4. ## (out) : J’appartiens  [A]
  5.  
  6. B.printItSelf (obj)
  7. ## (out) : J’appartiens  [B]

Assez surprenant comme methode … ( ; en fait on doit passer l’objet créé en parametre de notre methode, et appeler la methode a partir du nom de la classe, mais en y reflechissant ca peut paraitre logique vu la maniere dont on définit les classes sous Python. Il peut etre interessant pour ceux qui souhaitent apprendre un langage de programmation de commencer par Python (: ! de plus mixé avec la bibliotheque pyGTK on arrive d’excellents programmes de qualité merci au channel #gnomefr pour l’astuce.

5 commentaires pour Polymorphisme sous Python

  1. Matt dit :

    Ah je savais pas que Python supportait le polymorphisme, ça c’est cool c’est rare :)

    Merci sahid ;)

  2. Sahid dit :

    Oui,
    J’ai vu qu’on parle de surcharge de fonctions pour python mais pas de polymorphisme pure.

    Enfin bon comme ca c dis ( ;

  3. steve dit :

    # 1.
    class A:
    2.
    ???? Classe A ?????
    3.
    printItSelf (self):
    4.
    print ??J??appartiens [A]?;
    5.

    6.
    class B:
    7.
    ???? Classe B ?????
    8.
    printItSelf (self):
    9.
    print ??J??appartiens [B]?;
    10.

    11.
    class C (A, B):
    12.
    ???? Classe C, herite de A et B ?????
    13.
    printItSelf (self):
    14.
    print ??J??appartiens [C]?;

    Nous allons maintenant voir comment appeler la methode printItSelf de A et B a partir de notre instance de C

    1.
    C obj # Instance de C( compren pas le “obj”)
    2.

    3.
    A.printItSelf (obj)
    4.
    ## (out) : J??appartiens [A]
    5.

    6.
    B.printItSelf (obj)
    7.
    ## (out) : J??appartiens [B]
    8.

    j’suis un petit novice et j’ai besoin que tu précise quoi correspond le “obj” qui apparaitr aprÚs le c ligne 1, merci pour ta réponse

  4. sahid dit :

    Salut Steve,

    obj, est un objet de la classe C.

    En faite tu passes en premier paramÚtre de ta methode l’objet créer,
    et si tu remarques bien, pour chacune des méthodes d’une classe en python
    on attend obligatoirement un paramÚtre (self par convention).

    sahid

  5. loic dit :

    priori il faut remplacer

    C obj # Instance de C

    par

    obj = C() # Instance de C

    et de plus tu donnes un exemple pour:

    “Nous allons maintenant voir comment appeler la methode printItSelf de A et B a partir de notre instance de C”

    ce qui permet de résoudre un problÚme posé par l’héritage multiple, mais qui n’est pas forcément lié au polymorphisme …

    plus d’explication sur le polymorphisem par exemple l”daresse: http://sebsauvage.net/comprendre/objet/objet_heritage.html

Ajouter un commentaire pour Polymorphisme sous Python

  • levitra versus cialis
  • ingredient in phentermine
  • lipitor drug
  • generic trileptal
  • januvia 100 mg
  • gabapentin 100 mg
  • new anxiety drugs
  • keppra
  • reglan 10 mg
  • order prednisolone
  • fluticasone propionate
  • rogaine coupons
  • yasmin prescription
  • singulair 5 mg
  • sexual stamina tips
  • alavert drug
  • revatio 20 mg
  • lexapro drugs
  • european pharmacy anxiety
  • attacking anxiety and depression
  • cheap retin a
  • drugs for depression
  • cialis table
  • singulair 10 mg
  • penis enlargement exercises
  • cephalexin sinus
  • phentermine no prescription
  • order nolvadex
  • green tea pill
  • cialis advertisement
  • supplement hoodia
  • cheap viamax
  • valium without prescription
  • fosamax generic
  • cialis name brand cheap
  • ambien dosage
  • viagra and blood pressure
  • xanax rx
  • order viagra without prescription
  • atacand
  • buy prandin
  • cheap yohimbe
  • half life of valium
  • order capoten
  • alprazolam cheap
  • buy cialis by check
  • vitamins for erectile dysfunction
  • klonopin medication
  • drugs used to treat depression
  • online claritin
  • buy cialis online uk
  • benadryl loratadine
  • anxiety meds online
  • asthma in elderly
  • propecia 1mg
  • levitra vardenafil generic
  • cialis online discount
  • order cialis professional
  • clomid treatment
  • medicine xanax
  • methylsulfonylmethane
  • xanax mechanism of action
  • pharmacy online australia
  • cialis vs viagra
  • order lisinopril
  • viagra online best price
  • increase penis size with herbs
  • oxybutynin
  • bayer levitra online pharmacy
  • phentermine consultation
  • free levitra samples
  • acticin cream
  • ventolin inhalador
  • natural viagra substitute
  • glipizide side effects
  • benadryl dosing
  • new treatments for lung diseases
  • viagra jelly
  • types of antidepressants
  • perennial allergic rhinitis
  • mebendazole
  • levitra ad
  • cheap hangover helper
  • interaction zocor
  • cheapest cialis generic
  • cialis cost low
  • clonazepam .5mg
  • kamagra oral jelly
  • ativan no prescription
  • gerd in children
  • prozac social anxiety
  • antidepressant pill high
  • how to cure depression
  • use imitrex
  • cheap robaxin
  • what are the effects of klonopin
  • how to buy viagra online
  • coral calcium
  • canada in levitra
  • cheap viagra pill
  • discount levitra purchase
  • buy levothroid
  • purchase vardenafil
  • online tramadol
  • cialis dosage 20mg
  • viagra soft tab
  • norvasc 10
  • sample ambien
  • lamictal drug
  • herb echinacea
  • effects side ultram
  • endep
  • bayer levitra professional pro
  • addiction ultram
  • giant eagle pharmacy
  • benfotiamine
  • green tea capsule
  • cheap male enhancement
  • order caffeine
  • how much is viagra
  • drug ceftin
  • sumatriptan
  • cialis tablet
  • danazol
  • soma the drug
  • medication for bipolar disorder
  • cozaar medication
  • recreational use of xanax
  • cheap avandia
  • zithromax medication
  • buy lorazepam without prescription
  • reglan side effects
  • drug skelaxin
  • canada pharmacy viagra
  • buy zelnorm
  • phentermine no prescription overnight
  • impotence depression
  • buy ampicillin
  • levitra for sale
  • viagra patent lawsuit levitra
  • cialis vs viagra vs levitra
  • metformin hcl 500mg
  • ways to relieve anxiety
  • order rimonabant
  • natural prednisone
  • generic orlistat
  • nitroglycerin sublingual
  • wellbutrin paxil
  • schizophrenia drugs new zealand
  • lasix furosemide
  • unisom online
  • viagra half price pharmacy
  • manufacturer of revatio
  • clomiphene tablets
  • valium low cost
  • vpxl uk
  • ibuprofen
  • buy probenecid
  • buy etodolac
  • cialis canadian generic
  • stop smoking gum
  • order topamax
  • celebrex pharmacy
  • stop smoking zyban
  • purchase cheap cialis online
  • children aspirin
  • treatment for impotence
  • cialis line order
  • what prednisone
  • buy cialis by the pill
  • buying ultram no prescription
  • venlafaxin
  • buy generic tramadol no prescription
  • allegra side effects
  • prescription nexium
  • order starlix
  • serevent
  • soft tab viagra
  • effects of premarin
  • cheap lovastatin
  • order amantadine
  • viagra and cialis
  • discount levitra online
  • meds for erectile dysfunction
  • sale carisoprodol
  • effects propecia
  • on rx legal diazepam
  • effects of viagra on women
  • buy generic allergy medication
  • ciallis or viagra
  • cheap imitrex
  • order amoxicillin
  • ativan normal doses
  • generic cialis soft
  • purchase phentermine mexico
  • saw palmetto products
  • buy ativan online
  • discount soma online
  • allegra order
  • genric viagra