

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)

class MyClass:
    """A simple example class"""
    i = 12345

    def f(self):
        return 'hello world'
    
    def __init__(self):
        self.data = [1,2,3]
        print(self.data)


class Complex:
    def __init__(self, realpart, imagpart):
        self.r = realpart
        self.i = imagpart

x = Complex(3.0, -4.5)
print(x.r, x.i)

x.counter = 1
while x.counter < 10:
    x.counter = x.counter * 2
print(x.counter)
del x.counter
# print(x.counter)

x = MyClass()
x.f()
xf = x.f
while True:
    print(xf())
    break

class Dog:

    kind = 'canine'         # class variable shared by all instances

    def __init__(self, name):
        self.name = name    # instance variable unique to each instance

d = Dog('Fido')
e = Dog('Buddy')
print(d.kind)               # shared by all dogs
'canine'
print(e.kind)               # shared by all dogs
'canine'
print(d.name)                  # unique to d
'Fido'
print(e.name)                  # unique to e
'Buddy'

class Dog2:

    tricks = []             # mistaken use of a class variable

    def __init__(self, name):
        self.name = name

    def add_trick(self, trick):
        self.tricks.append(trick)

d = Dog2('Fido')
e = Dog2('Buddy')
d.add_trick('roll over')
e.add_trick('play dead')
print(d.tricks)                # unexpectedly shared by all dogs
['roll over', 'play dead']
print(e.tricks)

class Dog3:

    def __init__(self, name):
        self.name = name
        self.tricks = []    # creates a new empty list for each dog3

    def add_trick(self, trick):
        self.tricks.append(trick)

d = Dog3('Fido')
e = Dog3('Buddy')
d.add_trick('roll over')
e.add_trick('play dead')
print(d.tricks)
['roll over']
print(e.tricks)
['play dead']

# Function defined outside the class
def f1(self, x, y):
    return min(x, x+y)

class C:
    f = f1

    def g(self):
        return 'hello world'

    h = g

class Bag:
    def __init__(self):
        self.data = []

    def addtwice(self, x):
        self.add(x)
        self.add(x)

    def add(self, x):
        self.data.append(x)

b = Bag()
b.add(5)
b.add("c")
print(b.data)

class Mapping:
    def __init__(self, iterable):
        self.items_list = []
        self.__update(iterable)

    def update(self, iterable):
        for item in iterable:
            self.items_list.append(item)

    __update = update   # private copy of original update() method

class MappingSubclass(Mapping):

    def update(self, keys, values):
        # provides new signature for update()
        # but does not break __init__()
        for item in zip(keys, values):
            self.items_list.append(item)

class Employee:
    pass

john = Employee()  # Create an empty employee record

# Fill the fields of the record
john.name = 'John Doe'
john.dept = 'computer lab'
john.salary = 1000

for element in [1, 2, 3]:
    print(element)
for element in (1, 2, 3):
    print(element)
for key in {'one':1, 'two':2}:
    print(key)
for char in "123":
    print(char)
for line in open("myfile.txt"):
    print(line, end='')

s = 'abc'
it = iter(s)
it

next(it)

next(it)

next(it)

# next(it)

class Reverse:
    """Iterator for looping over a sequence backwards."""
    def __init__(self, data):
        self.data = data
        self.index = len(data)

    def __iter__(self):
        return self

    def __next__(self):
        if self.index == 0:
            raise StopIteration
        self.index = self.index - 1
        return self.data[self.index]

rev = Reverse('spam')
iter(rev)

for char in rev:
    print(char)
70e94747f5ccffe1080ec600b2b26c081fc5f844
<mask token>


class Dog:
    <mask token>

    def __init__(self, name):
        self.name = name


<mask token>


class Dog2:
    tricks = []

    def __init__(self, name):
        self.name = name

    def add_trick(self, trick):
        self.tricks.append(trick)


<mask token>


class Dog3:

    def __init__(self, name):
        self.name = name
        self.tricks = []

    def add_trick(self, trick):
        self.tricks.append(trick)


<mask token>


class C:
    f = f1

    def g(self):
        return 'hello world'
    h = g


class Bag:

    def __init__(self):
        self.data = []

    def addtwice(self, x):
        self.add(x)
        self.add(x)

    def add(self, x):
        self.data.append(x)


<mask token>


class Mapping:

    def __init__(self, iterable):
        self.items_list = []
        self.__update(iterable)

    def update(self, iterable):
        for item in iterable:
            self.items_list.append(item)
    __update = update


class MappingSubclass(Mapping):

    def update(self, keys, values):
        for item in zip(keys, values):
            self.items_list.append(item)


class Employee:
    pass


<mask token>


class Reverse:
    """Iterator for looping over a sequence backwards."""

    def __init__(self, data):
        self.data = data
        self.index = len(data)

    def __iter__(self):
        return self

    def __next__(self):
        if self.index == 0:
            raise StopIteration
        self.index = self.index - 1
        return self.data[self.index]


<mask token>

<mask token>


class MyClass:
    <mask token>
    <mask token>
    <mask token>
    <mask token>


class Complex:

    def __init__(self, realpart, imagpart):
        self.r = realpart
        self.i = imagpart


<mask token>


class Dog:
    kind = 'canine'

    def __init__(self, name):
        self.name = name


<mask token>


class Dog2:
    tricks = []

    def __init__(self, name):
        self.name = name

    def add_trick(self, trick):
        self.tricks.append(trick)


<mask token>


class Dog3:

    def __init__(self, name):
        self.name = name
        self.tricks = []

    def add_trick(self, trick):
        self.tricks.append(trick)


<mask token>


class C:
    f = f1

    def g(self):
        return 'hello world'
    h = g


class Bag:

    def __init__(self):
        self.data = []

    def addtwice(self, x):
        self.add(x)
        self.add(x)

    def add(self, x):
        self.data.append(x)


<mask token>


class Mapping:

    def __init__(self, iterable):
        self.items_list = []
        self.__update(iterable)

    def update(self, iterable):
        for item in iterable:
            self.items_list.append(item)
    __update = update


class MappingSubclass(Mapping):

    def update(self, keys, values):
        for item in zip(keys, values):
            self.items_list.append(item)


class Employee:
    pass


<mask token>


class Reverse:
    """Iterator for looping over a sequence backwards."""

    def __init__(self, data):
        self.data = data
        self.index = len(data)

    def __iter__(self):
        return self

    def __next__(self):
        if self.index == 0:
            raise StopIteration
        self.index = self.index - 1
        return self.data[self.index]


<mask token>

<mask token>


class MyClass:
    <mask token>
    <mask token>

    def f(self):
        return 'hello world'

    def __init__(self):
        self.data = [1, 2, 3]
        print(self.data)


class Complex:

    def __init__(self, realpart, imagpart):
        self.r = realpart
        self.i = imagpart


<mask token>


class Dog:
    kind = 'canine'

    def __init__(self, name):
        self.name = name


<mask token>


class Dog2:
    tricks = []

    def __init__(self, name):
        self.name = name

    def add_trick(self, trick):
        self.tricks.append(trick)


<mask token>


class Dog3:

    def __init__(self, name):
        self.name = name
        self.tricks = []

    def add_trick(self, trick):
        self.tricks.append(trick)


<mask token>


class C:
    f = f1

    def g(self):
        return 'hello world'
    h = g


class Bag:

    def __init__(self):
        self.data = []

    def addtwice(self, x):
        self.add(x)
        self.add(x)

    def add(self, x):
        self.data.append(x)


<mask token>


class Mapping:

    def __init__(self, iterable):
        self.items_list = []
        self.__update(iterable)

    def update(self, iterable):
        for item in iterable:
            self.items_list.append(item)
    __update = update


class MappingSubclass(Mapping):

    def update(self, keys, values):
        for item in zip(keys, values):
            self.items_list.append(item)


class Employee:
    pass


<mask token>


class Reverse:
    """Iterator for looping over a sequence backwards."""

    def __init__(self, data):
        self.data = data
        self.index = len(data)

    def __iter__(self):
        return self

    def __next__(self):
        if self.index == 0:
            raise StopIteration
        self.index = self.index - 1
        return self.data[self.index]


<mask token>

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)


class MyClass:
    """A simple example class"""
    i = 12345

    def f(self):
        return 'hello world'

    def __init__(self):
        self.data = [1, 2, 3]
        print(self.data)


class Complex:

    def __init__(self, realpart, imagpart):
        self.r = realpart
        self.i = imagpart


<mask token>
print(x.r, x.i)
<mask token>
while x.counter < 10:
    x.counter = x.counter * 2
print(x.counter)
del x.counter
<mask token>
x.f()
<mask token>
while True:
    print(xf())
    break


class Dog:
    kind = 'canine'

    def __init__(self, name):
        self.name = name


<mask token>
print(d.kind)
<mask token>
print(e.kind)
<mask token>
print(d.name)
<mask token>
print(e.name)
<mask token>


class Dog2:
    tricks = []

    def __init__(self, name):
        self.name = name

    def add_trick(self, trick):
        self.tricks.append(trick)


<mask token>
d.add_trick('roll over')
e.add_trick('play dead')
print(d.tricks)
['roll over', 'play dead']
print(e.tricks)


class Dog3:

    def __init__(self, name):
        self.name = name
        self.tricks = []

    def add_trick(self, trick):
        self.tricks.append(trick)


<mask token>
d.add_trick('roll over')
e.add_trick('play dead')
print(d.tricks)
['roll over']
print(e.tricks)
['play dead']


def f1(self, x, y):
    return min(x, x + y)


class C:
    f = f1

    def g(self):
        return 'hello world'
    h = g


class Bag:

    def __init__(self):
        self.data = []

    def addtwice(self, x):
        self.add(x)
        self.add(x)

    def add(self, x):
        self.data.append(x)


<mask token>
b.add(5)
b.add('c')
print(b.data)


class Mapping:

    def __init__(self, iterable):
        self.items_list = []
        self.__update(iterable)

    def update(self, iterable):
        for item in iterable:
            self.items_list.append(item)
    __update = update


class MappingSubclass(Mapping):

    def update(self, keys, values):
        for item in zip(keys, values):
            self.items_list.append(item)


class Employee:
    pass


<mask token>
for element in [1, 2, 3]:
    print(element)
for element in (1, 2, 3):
    print(element)
for key in {'one': 1, 'two': 2}:
    print(key)
for char in '123':
    print(char)
for line in open('myfile.txt'):
    print(line, end='')
<mask token>
it
next(it)
next(it)
next(it)


class Reverse:
    """Iterator for looping over a sequence backwards."""

    def __init__(self, data):
        self.data = data
        self.index = len(data)

    def __iter__(self):
        return self

    def __next__(self):
        if self.index == 0:
            raise StopIteration
        self.index = self.index - 1
        return self.data[self.index]


<mask token>
iter(rev)
for char in rev:
    print(char)



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)

class MyClass:
    """A simple example class"""
    i = 12345

    def f(self):
        return 'hello world'
    
    def __init__(self):
        self.data = [1,2,3]
        print(self.data)


class Complex:
    def __init__(self, realpart, imagpart):
        self.r = realpart
        self.i = imagpart

x = Complex(3.0, -4.5)
print(x.r, x.i)

x.counter = 1
while x.counter < 10:
    x.counter = x.counter * 2
print(x.counter)
del x.counter
# print(x.counter)

x = MyClass()
x.f()
xf = x.f
while True:
    print(xf())
    break

class Dog:

    kind = 'canine'         # class variable shared by all instances

    def __init__(self, name):
        self.name = name    # instance variable unique to each instance

d = Dog('Fido')
e = Dog('Buddy')
print(d.kind)               # shared by all dogs
'canine'
print(e.kind)               # shared by all dogs
'canine'
print(d.name)                  # unique to d
'Fido'
print(e.name)                  # unique to e
'Buddy'

class Dog2:

    tricks = []             # mistaken use of a class variable

    def __init__(self, name):
        self.name = name

    def add_trick(self, trick):
        self.tricks.append(trick)

d = Dog2('Fido')
e = Dog2('Buddy')
d.add_trick('roll over')
e.add_trick('play dead')
print(d.tricks)                # unexpectedly shared by all dogs
['roll over', 'play dead']
print(e.tricks)

class Dog3:

    def __init__(self, name):
        self.name = name
        self.tricks = []    # creates a new empty list for each dog3

    def add_trick(self, trick):
        self.tricks.append(trick)

d = Dog3('Fido')
e = Dog3('Buddy')
d.add_trick('roll over')
e.add_trick('play dead')
print(d.tricks)
['roll over']
print(e.tricks)
['play dead']

# Function defined outside the class
def f1(self, x, y):
    return min(x, x+y)

class C:
    f = f1

    def g(self):
        return 'hello world'

    h = g

class Bag:
    def __init__(self):
        self.data = []

    def addtwice(self, x):
        self.add(x)
        self.add(x)

    def add(self, x):
        self.data.append(x)

b = Bag()
b.add(5)
b.add("c")
print(b.data)

class Mapping:
    def __init__(self, iterable):
        self.items_list = []
        self.__update(iterable)

    def update(self, iterable):
        for item in iterable:
            self.items_list.append(item)

    __update = update   # private copy of original update() method

class MappingSubclass(Mapping):

    def update(self, keys, values):
        # provides new signature for update()
        # but does not break __init__()
        for item in zip(keys, values):
            self.items_list.append(item)

class Employee:
    pass

john = Employee()  # Create an empty employee record

# Fill the fields of the record
john.name = 'John Doe'
john.dept = 'computer lab'
john.salary = 1000

for element in [1, 2, 3]:
    print(element)
for element in (1, 2, 3):
    print(element)
for key in {'one':1, 'two':2}:
    print(key)
for char in "123":
    print(char)
for line in open("myfile.txt"):
    print(line, end='')

s = 'abc'
it = iter(s)
it

next(it)

next(it)

next(it)

# next(it)

class Reverse:
    """Iterator for looping over a sequence backwards."""
    def __init__(self, data):
        self.data = data
        self.index = len(data)

    def __iter__(self):
        return self

    def __next__(self):
        if self.index == 0:
            raise StopIteration
        self.index = self.index - 1
        return self.data[self.index]

rev = Reverse('spam')
iter(rev)

for char in rev:
    print(char)