Python3 tutorial day4 part 2

Classes

 

__author__ = 'andy'

def scope_test():
def do_local():
spam = "local spam"
def do_nonlocal():
nonlocal spam
spam = "nonlocal spam"
def do_global():
global spam
spam = "global spam"
spam = "test spam"
do_local()
print("After local assignment:", spam)
do_nonlocal()
print("After nonlocal assignment:", spam)
do_global()
print("After global assignment:", spam)

scope_test()
print("In global scope:", spam)

 

After local assignment: test spam
After nonlocal assignment: nonlocal spam
After global assignment: nonlocal spam
In global scope: global spam

 

class syntax

 

class ClassName:
    <statement-1>
    .
    .
    .
    <statement-N>



class Object

Class objects support two kinds of operations: attribute references and instantiation
Class has a init method
def __init__(self):

class and instance variables

'__' 为私有函数
首尾双下划线是专有函数

Inheritance

 

class DerivedClassName(BaseClassName):
    <statement-1>
    .
    .
    .
    <statement-N>

isinstance()
issubclass()   
 

posted on 2014-10-23 17:30  ukouryou  阅读(112)  评论(0编辑  收藏  举报

导航