Transcript
  • 7/29/2019 Lecture#3: Introductory Python

    1/25

    High performance computing

    for non-programmers

    Lecture #3:Python programminglanguage: Overview

    Glib Ivashkevych

    junior researcher, NSC KIPT

  • 7/29/2019 Lecture#3: Introductory Python

    2/25

    Functions

    default arguments

    defmy_func(a, b=None):

    print a

    print b

    my_func(1) #the same as my_func(1, None)

    my_func(1, 2)

  • 7/29/2019 Lecture#3: Introductory Python

    3/25

    Functions

    callsdefmy_func(a, b=None):

    print a

    print b

    my_func(b=2, a=1)

    my_func(b=2, 1) #SyntaxError

    l = [1,2]; my_func(*l) #Unpacking

    d = {a:1, b:2}; my_func(**d) #Unpacking

  • 7/29/2019 Lecture#3: Introductory Python

    4/25

    Functions

    variable number of args

    deffunc(a, *args):

    print a

    print args

    func(1)

    func(1, 2, 3)

    func(1, *(2,3))

    func(1, *[2,3]); func(1, [2,3]) #Not the same!

  • 7/29/2019 Lecture#3: Introductory Python

    5/25

    Functions

    altogether

    deffunc(a, *args, **kwargs):

    print a

    print args

    print kwargs

    func(1, 2, 3, name=name)

    func(1, *[2,3], **{name:name})

    func(1, **{name:name}, *[2,3]) #SyntaxError

  • 7/29/2019 Lecture#3: Introductory Python

    6/25

    Functions

    tricky stufffunctions are objects

    created when defis encountered

    we can:

    add attributes to functions

    assign them

    return them

    names in functions are local, except whenglobal instruction is used

  • 7/29/2019 Lecture#3: Introductory Python

    7/25

    Classes

    classes are objects too (oh, man!)

    created when class is encountered

    class Primitive(object):

    pass

    we can create classes at runtime

  • 7/29/2019 Lecture#3: Introductory Python

    8/25

    Classes

    constructors & attributesclass Primitive(object):

    def__init__(self):

    pass

    class Primitive(object):

    field = 5. #class attribute?

    def__init__(self, num):

    self.field = num #Nope!

  • 7/29/2019 Lecture#3: Introductory Python

    9/25

    Classes

    constructors & attributesclass Primitive(object):

    field = 5. #class attribute?

    def__init__(self, num):

    Primitive.field = num #Yeap!

    x = Primitive(1) #is the same as

    x = Primitive.__new__(Primitive, 1)

  • 7/29/2019 Lecture#3: Introductory Python

    10/25

    Classes

    methodsclass Primitive(object):

    field = 5.

    defget_field(self):

    return self.field

    x = Primitive()

    f = x.get_field()

  • 7/29/2019 Lecture#3: Introductory Python

    11/25

    Classes

    inheritanceclass Primitive(object):

    field = 5.

    defget_field(self):

    return self.field

    class NewPrimitive(Primitive):

    defget_field(self):

    print self.field

    return self.field

  • 7/29/2019 Lecture#3: Introductory Python

    12/25

    Classes

    inheritanceNewPrimitive can be used instead of Primitive

    Child classes can

    add functionality to parent class

    change functionality of parent class

    NewPrimitive is Primitive

  • 7/29/2019 Lecture#3: Introductory Python

    13/25

    Classes

    multiple inheritanceclass Primitive(object):

    field = 5.

    class AnotherPrimitive(object):

    anotherfield = 10.

    class Derived(Primitive, AnotherPrimitive):

    defget_fields(self):

    print field

    print anotherfield

  • 7/29/2019 Lecture#3: Introductory Python

    14/25

    Classes

    multiple inheritance

    No conflicts - no problems

  • 7/29/2019 Lecture#3: Introductory Python

    15/25

    Classes

    polymorphismclass LinearAccelerator(object):

    defcollide(self):

    print I use steady target

    class Collider(object):

    defcollide(self):

    print I use two beams

    Both can collide. But differently.

  • 7/29/2019 Lecture#3: Introductory Python

    16/25

    Classes

    encapsulationRefer to:

    hiding datainside classes(i.e. privatemethods and attrs)

    bundling data and operations on itinsideclasses

  • 7/29/2019 Lecture#3: Introductory Python

    17/25

    Classes

    encapsulationclass Cl(object):

    _field = 5. #indicates for internal use

    __priv = 10. #magled, i.e. __Cl_priv

    Encapsulation is mostly bundling in Python

  • 7/29/2019 Lecture#3: Introductory Python

    18/25

    Classes

    compositionclass P(object):

    pass

    class R(object):

    defset_P(self, P):

    self.P = P

    R has or is composed of P

  • 7/29/2019 Lecture#3: Introductory Python

    19/25

    Modules

    A way to structure code

    #a.py:

    class P(object):

    pass

    #b.py

    import a

    p = a.P()

  • 7/29/2019 Lecture#3: Introductory Python

    20/25

    Modules

    import syntax

    import

    import as

    from import ,

    from import *

    from import as

  • 7/29/2019 Lecture#3: Introductory Python

    21/25

    Modules

    modules search order

    application home folder

    PYTHONPATH environment variable

    standard lib folders

    (sys.path)

  • 7/29/2019 Lecture#3: Introductory Python

    22/25

    Modules

    import process

    search

    executehigh level - executed immediately, inner level - when called

    Skipped if module was already loaded (sys.modules)

  • 7/29/2019 Lecture#3: Introductory Python

    23/25

    Packages

    Another structuring mechanism: to organizemodules

    Folder must contain __init__.py to be treated aspackagefolder a: __init__.py, module.py

    folder b: another_module.py

    import a #OK

    import a as somename #OK

    import b#Nope!

  • 7/29/2019 Lecture#3: Introductory Python

    24/25

    Zen of Python

    import thisThe Zen of Python, by Tim Peters

    Beautiful is better than ugly.

    Explicit is better than implicit.Simple is better than complex.Complex is better than complicated.Flat is better than nested.Sparse is better than dense.Readability counts.Special cases aren't special enough to break the rules.Although practicality beats purity.Errors should never pass silently.

    Unless explicitly silenced.In the face of ambiguity, refuse the temptation to guess.There should be one-- and preferably only one --obvious way to do it.Although that way may not be obvious at first unless you're Dutch.Now is better than never.Although never is often better than *right* now.If the implementation is hard to explain, it's a bad idea.If the implementation is easy to explain, it may be a good idea.Namespaces are one honking great idea -- let's do more of those!

  • 7/29/2019 Lecture#3: Introductory Python

    25/25

    Questions?


Recommended