zoukankan      html  css  js  c++  java
  • cs61a Mutable Data 1 学习笔记和补充

    CS61A Spring 2018
    原文地址:http://composingprograms.com/pages/24-mutable-data.html
    视频地址 :
    https://www.youtube.com/watch?v=Dd2AxpOiHGg&list=PL6BsET-8jgYUqH93Ik4w5Rk-A3qGMhhq0&index=4


    One powerful technique for creating modular programs is to incorporate data that may change state over time. Adding state to data is a central ingredient of a paradigm called object-oriented programming.

    The Object Metaphor

    The difference between functions and data: functions performed operations and data were operated upon.
    Objects combine data values with behavior.
    Objects are both information and processes, bundled together to represent the properties, interactions, and behaviors of complex things.
    Object behavior is implemented in Python through specialized object syntax and associated terminology。
    object
    like many other programming languages, we use dot notation to designated an attribute of an object.

    Sequence Objects

    introduce the operation by a story.

    the develop of the chinese cards to Playing cards:

    >>> chinese = ['coin', 'string', 'myriad']  # A list literal
    >>> suits = chinese                         # Two names refer to the same list

    As cards migrated to Europe (perhaps through Egypt), only the suit of coins remained in Spanish decks (oro).

    >>> suits.pop()             # Remove and return the final element
    'myriad'
    >>> suits.remove('string')  # Remove the first element that equals the arguments 

    Three more suits were added (they evolved in name and design over time),

    >>> suits.append('cup')              # Add an element to the end
    >>> suits.extend(['sword', 'club'])  # Add all elements of a sequence to the end

    and Italians called swords spades.

    >>> suits[2] = 'spade'  # Replace an element

    giving the suits of a traditional Italian deck of cards.

    >>> suits
    ['coin', 'cup', 'spade', 'club']

    The French variant used today in the U.S. changes the first two suits:

    >>> suits[0:2] = ['heart', 'diamond']  # Replace a slice
    >>> suits
    ['heart', 'diamond', 'spade', 'club']

    Methods also exist for inserting, sorting, and reversing lists. All of these mutation operations change the value of the list; they do not create new list objects.
    这里写图片描述

    comparison operators : is and is not

    Sameness and Change

    change

    evaluate to the identical object

    Identity is a stronger condition than equality.

    >>> suits is nest[0]
    True
    >>> suits is ['heart', 'diamond', 'spade', 'club']
    False
    >>> suits == ['heart', 'diamond', 'spade', 'club']
    True

    The former checks for identity, while the latter checks for the equality of contents.

    slice

    Slicing a list creates a new list and leaves the original list unchanged.
    a new list is constructed that contains a subset of the same values as the sliced list.
    Slicing a list with subset

    the different between extend and append:

    extend_append

    the pop and remove

    pop - it removes and returns the element at index i of the list.
    remove - It removes the first item in the list that is equal to its argument.
    the argument is difference, one is index, the other is element.

    etc

    Tuples

    A tuple, an instance of the built-in tuple type, is an immutable sequence.
    Any objects can be placed within tuples.

    >>> ()    # 0 elements
    ()
    >>> (10,) # 1 element
    (10,)

    tuples

    While it is not possible to change which elements are in a tuple, it is possible to change the value of a mutable element contained within a tuple.
    tuple-list

    Dictionaries

    for storing and manipulating correspondence relationships
    A dictionary contains key-value pairs, where both the keys and values are objects.
    Adding new key-value pairs and changing the existing value for a key can both be achieved with assignment statements.

    >>> numerals = {'I': 1.0, 'V': 5, 'X': 10}
    >>> numerals['I'] = 1
    >>> numerals['L'] = 50
    >>> numerals
    {'I': 1, 'X': 10, 'L': 50, 'V': 5}

    Dictionaries1
    Notice that ‘L’ was not added to the end of the output above. Dictionaries are unordered collections of key-value pairs.
    Dictionaries2

    restrictions:
    A key of a dictionary cannot be or contain a mutable value.
    There can be at most one value for a given key.

    Local state

    Lists and dictionaries have local state, the word “state” implies an evolving process.

    nonlocal statements

    nonlocal0
    nonlocal1
    nonlocal2

    it’s critical to understand that all instances of a name must refer to the same frame.
    instance_and_frame

    Two bindings for the name balance in two different frames, and each withdraw function has a different parent.
    nonlocal

  • 相关阅读:
    MFC之绘制线条
    CDC类详解
    MFC之消息映射机制实现方法
    VS2008如何自动添加消息映射
    MFC框架程序剖析
    Visual Assist 相同内容高亮显示
    Win32 Console Application、Win32 Application、MFC三者之间的联系和区别
    win32应用程序创建流程
    BigDecimal最基础用法【转】
    html 高亮显示表格当前行【转】
  • 原文地址:https://www.cnblogs.com/siucaan/p/9623181.html
Copyright © 2011-2022 走看看