菜单

react-navigation 使用详解

2019年4月23日 - 金沙前端

大旨测试函数:

JavaScript

function test(dateString,times,func){ var
startTime=window.performance.now(); //
console.log(‘start=’+start提姆e.getTime()); for (var i = 0; i < times;
i++) { func(dateString);//这里填写具体的解析函数 }; var
endTime=window.performance.now(); //
console.log(‘endTime=’+endTime.getTime()); var
gapTime=endTime-startTime; console.log(‘壹共耗费时间:’+gapTime+’ms’); //
console.log(‘时间字符串’+dateString); return gapTime; }

1
2
3
4
5
6
7
8
9
10
11
12
13
    function test(dateString,times,func){
    var startTime=window.performance.now();
    // console.log(‘start=’+startTime.getTime());
    for (var i = 0; i < times; i++) {
        func(dateString);//这里填写具体的解析函数
    };
    var endTime=window.performance.now();
    // console.log(‘endTime=’+endTime.getTime());
    var gapTime=endTime-startTime;
      console.log(‘一共耗时:’+gapTime+’ms’);
    // console.log(‘时间字符串’+dateString);
    return gapTime;
}

 

故而那里用window.performance.now()而不用new Date(),是因为前者纯粹度远比后者高。后者只能正确到ms。会对结果形成不小影响

 

在Python中,大多对象都以可以经过for语句来一向遍历的,比如list、string、dict等等,这一个目的都得以被叫作可迭代对象。至于说什么样对象是足以被迭代访问的,就要打听一下迭代器相关的知识了。



 另一个大娘写的关于抽屉的博文:

后天参见大灰狼得博客,好好得总计一下navigation得用法。以前老是看官方文书档案,但知情得不太透测。

一、开源库介绍

 

 

上一年6月份,新开源的React-natvigation库让人惊叹。在不久不到八个月的日子,github上星数已达四千+。Fb推荐使用库,并且在React
Native此时此刻风靡版本0.4四上校Navigator删除。react-navigation据称有原生般的品质体验效果。恐怕会成为以往React
Native导航组件的主流军。本篇内容听别人说【
^一.0.0-beta.9】版本来介绍有关该库的使用和实战技艺。能够看到,即使是beta版本,可是基本平静,我们可放心在品种中利用。该库包罗三类组件:

style=”color: #9八千0;”>(1)StackNavigator:用来跳转页面和传递参数

style=”font-family: Arial; font-size: 1四px; line-height: 二陆px; color: #9玖仟0;”> style=”font-family: Arial; font-size: 1肆px; line-height: 二陆px; color: #970000;”>(2)TabNavigator:类似尾部导航栏,用来在平等显示器下切换区别分界面

style=”color: #9七千0;”>(叁)DrawerNavigator:侧滑菜单导航栏, id=”result_box” lang=”zh-CN”> title=”Used to easily set up a screen with a drawer navigation.”>用于轻便设置带抽屉导航的荧屏

二、react-navigation使用

style=”font-family: Arial; font-size: 14px; line-height: 26px; color: #990000;”> style=”font-family: Arial; font-size: 14px; line-height: 26px; color: #990000;”> style=”font-family: Arial; font-size: 14px; line-height: 26px;”>

具体内容大约分成如下:

(一)react-navigation库属性介绍

style=”color: #9捌仟0;”>(2)StackNavigator、TabNavigator实现分界面间跳转,Tab切换

style=”color: #980000;”>(3)StackNavigator分界面间跳转、传值、取值

(四)DrawerNavigator完毕抽屉导航菜单

(伍)DrawerNavigator扩大成效

(陆)修改源码,定制UI分界面

一、StackNavigator属性介绍

 navigationOptions:配置StackNavigator的一些属性。  
  
    title:标题,如果设置了这个导航栏和标签栏的title就会变成一样的,不推荐使用  
    header:可以设置一些导航的属性,如果隐藏顶部导航栏只要将这个属性设置为null  
    headerTitle:设置导航栏标题,推荐  
    headerBackTitle:设置跳转页面左侧返回箭头后面的文字,默认是上一个页面的标题。可以自定义,也可以设置为null  
    headerTruncatedBackTitle:设置当上个页面标题不符合返回箭头后的文字时,默认改成"返回"  
    headerRight:设置导航条右侧。可以是按钮或者其他视图控件  
    headerLeft:设置导航条左侧。可以是按钮或者其他视图控件  
    headerStyle:设置导航条的样式。背景色,宽高等  
    headerTitleStyle:设置导航栏文字样式  
    headerBackTitleStyle:设置导航栏‘返回’文字样式  
    headerTintColor:设置导航栏颜色  
    headerPressColorAndroid:安卓独有的设置颜色纹理,需要安卓版本大于5.0  
    gesturesEnabled:是否支持滑动返回手势,iOS默认支持,安卓默认关闭  
   
  
screen:对应界面名称,需要填入import之后的页面  
  
mode:定义跳转风格  
  
   card:使用iOS和安卓默认的风格  
  
   modal:iOS独有的使屏幕从底部画出。类似iOS的present效果  
  
headerMode:返回上级页面时动画效果  
  
   float:iOS默认的效果  
  
   screen:滑动过程中,整个页面都会返回  
  
   none:无动画  
  
cardStyle:自定义设置跳转效果  
  
   transitionConfig: 自定义设置滑动返回的配置  
  
   onTransitionStart:当转换动画即将开始时被调用的功能  
  
   onTransitionEnd:当转换动画完成,将被调用的功能  
  
path:路由中设置的路径的覆盖映射配置  
  
initialRouteName:设置默认的页面组件,必须是上面已注册的页面组件  
  
initialRouteParams:初始路由参数 

style=”color: #9八千0;”>注:我们或许对此path不老子@楚。path属性适用于 style=”color: #9七千0;”> style=”text-decoration: underline;”>其他app或浏览器采纳url张开本app并进入钦赐页面。path属性用于声圣元个界面路线,举例:【/pages/Home】。此时大家能够在手提式无线电话机浏览器中输入:app名称://pages/Home来运维该App,并跻身Home分界面。

二、TabNavigator属性介绍

 screen:和导航的功能是一样的,对应界面名称,可以在其他页面通过这个screen传值和跳转。  
  
  
navigationOptions:配置TabNavigator的一些属性  
  
title:标题,会同时设置导航条和标签栏的title  
  
tabBarVisible:是否隐藏标签栏。默认不隐藏(true)  
  
tabBarIcon:设置标签栏的图标。需要给每个都设置  
  
tabBarLabel:设置标签栏的title。推荐  
  
导航栏配置  
  
tabBarPosition:设置tabbar的位置,iOS默认在底部,安卓默认在顶部。(属性值:'top','bottom')  
  
swipeEnabled:是否允许在标签之间进行滑动  
  
animationEnabled:是否在更改标签时显示动画  
  
lazy:是否根据需要懒惰呈现标签,而不是提前,意思是在app打开的时候将底部标签栏全部加载,默认false,推荐为true  
  
trueinitialRouteName: 设置默认的页面组件  
  
backBehavior:按 back 键是否跳转到第一个Tab(首页), none 为不跳转  
  
tabBarOptions:配置标签栏的一些属性iOS属性  
  
activeTintColor:label和icon的前景色 活跃状态下  
  
activeBackgroundColor:label和icon的背景色 活跃状态下  
  
inactiveTintColor:label和icon的前景色 不活跃状态下  
  
inactiveBackgroundColor:label和icon的背景色 不活跃状态下  
  
showLabel:是否显示label,默认开启 style:tabbar的样式  
  
labelStyle:label的样式安卓属性  
  
activeTintColor:label和icon的前景色 活跃状态下  
  
inactiveTintColor:label和icon的前景色 不活跃状态下  
  
showIcon:是否显示图标,默认关闭  
  
showLabel:是否显示label,默认开启 style:tabbar的样式  
  
labelStyle:label的样式 upperCaseLabel:是否使标签大写,默认为true  
  
pressColor:material涟漪效果的颜色(安卓版本需要大于5.0)  
  
pressOpacity:按压标签的透明度变化(安卓版本需要小于5.0)  
  
scrollEnabled:是否启用可滚动选项卡 tabStyle:tab的样式  
  
indicatorStyle:标签指示器的样式对象(选项卡底部的行)。安卓底部会多出一条线,可以将height设置为0来暂时解决这个问题  
  
labelStyle:label的样式  
  
iconStyle:图标样式 

三、DrawerNavigator属性介绍

    DrawerNavigatorConfig  
      
        drawerWidth - 抽屉的宽度  
        drawerPosition - 选项是左或右。 默认为左侧位置  
        contentComponent - 用于呈现抽屉内容的组件,例如导航项。 接收抽屉的导航。 默认为DrawerItems  
        contentOptions - 配置抽屉内容  
      
        initialRouteName - 初始路由的routeName  
        order - 定义抽屉项目顺序的routeNames数组。  
        路径 - 提供routeName到路径配置的映射,它覆盖routeConfigs中设置的路径。  
        backBehavior - 后退按钮是否会切换到初始路由? 如果是,设置为initialRoute,否则为none。 默认为initialRoute行为  
      
       DrawerItems的contentOptions属性  
      
        activeTintColor - 活动标签的标签和图标颜色  
        activeBackgroundColor - 活动标签的背景颜色  
        inactiveTintColor - 非活动标签的标签和图标颜色  
        inactiveBackgroundColor - 非活动标签的背景颜色  
        内容部分的样式样式对象  
        labelStyle - 当您的标签是字符串时,要覆盖内容部分中的文本样式的样式对象  

style=”font-family: Arial; font-size: 1四px; line-height: 二陆px;”> style=”font-family: Arial; font-size: 1四px; line-height: 二陆px;”>从上述中山大学约掌握了react-navigation三种组件的某当中坚属性,所以到大家甩起袖子撸代码见证下神蹟了。

肆、使用StackNavigator + TabNavigator实现Tab分界面切换、界面间导航

 

API定义:StackNavigator(RouteConfigs,
StackNavigatorConfig)、TabNavigator(RouteConfigs,
TabNavigatorConfig)

(一)集成 react-navigation:在顶峰奉行 【 npm install react-navigation
–save 】

(贰)分界面中程导弹入须求组件:

    import {StackNavigator,TabNavigator,TabBarBottom} from 'react-navigation';  
    import HomeScreen from './pages/HomePage';  
    import MineScreen from './pages/MinePage';  

style=”font-family: Arial; font-size: 14px; line-height: 26px;”>(3)定义TabNavigator:

 

 const Tab = TabNavigator(  
  {  
    Home:{  
      screen:HomeScreen,  
      navigationOptions:({navigation}) => ({  
        tabBarLabel:'首页',  
        tabBarIcon:({focused,tintColor}) => (  
          <TabBarItem  
            tintColor={tintColor}  
            focused={focused}  
            normalImage={require('./imgs/nav_fav@2x.png')}  
            selectedImage={require('./imgs/nav_fav_actived@3x.png')}  
          />  
        )  
      }),  
    },  
  
    Mine:{  
          screen:MineScreen,  
          navigationOptions:({navigation}) => ({  
          tabBarLabel:'我',  
          tabBarIcon:({focused,tintColor}) => (  
            <TabBarItem  
             tintColor={tintColor}  
              focused={focused}  
              normalImage={require('./imgs/tab_me_nor@3x.png')}  
              selectedImage={require('./imgs/tab_me_selected@2x.png')}  
            />  
          )  
        }),  
      },  
    },  
  
    {  
      tabBarComponent:TabBarBottom,  
      tabBarPosition:'bottom',  
      swipeEnabled:false,  
      animationEnabled:false,  
      lazy:true,  
      tabBarOptions:{  
        activeTintColor:'#06c1ae',  
        inactiveTintColor:'#979797',  
        style:{backgroundColor:'#ffffff',},  
        labelStyle: {  
              fontSize: 20, // 文字大小  
          },  
      }  
        
    }  
  
  ); 

 

style=”font-family: Arial; font-size: 1四px; line-height: 二陆px;”>TabBarItem为包装的零部件:

 

    import React,{Component} from 'react';  
    import {Image} from 'react-native';  
      
    export default class TabBarItem extends Component {  
      
        render() {  
            return(  
                <Image source={ this.props.focused ? this.props.selectedImage : this.props.normalImage }  
                    style={ { tintColor:this.props.tintColor,width:25,height:25 } }  
                />  
            )  
        }  
          
    }  

 

 

 

style=”font-family: Arial; font-size: 14px; line-height: 2六px;”>能够看来,大家定义了贰个称号为【Tab】的TabNavigator的导航组件。在组件中,分为两层参数:

style=”font-family: Arial; font-size: 14px; line-height: 26px;”>(一)第3层参数定义了要切换的分界面,即【首页】、【小编】四个分界面组件,通过screen属性内定。并且通过navigationOptions属性设置相关属性参数。

style=”font-family: Arial; font-size: 1四px; line-height: 2陆px;”>(二)设置导航栏的习性参数。

style=”font-family: Arial; font-size: 1四px; line-height: 贰陆px;”>TabNavigator定义好之后,必要用StackNavigator,顾名思义,StackNavigator正是以栈的点子来存放整个分界面包车型大巴,而TabNavigator是作为三个分界面内区别子分界面之间切换。所以还亟需大家定义StackNavigator:

    const Navigator = StackNavigator(  
        
      {  
        Tab:{screen:Tab},  
        Product:{screen:ProductScreen}  
      },  
      
      {  
        navigationOptions:{  
          headerBackTitle:null,  
          headerTintColor:'#333333',  
          showIcon:true,  
         swipeEnabled:false,  
         animationEnabled:false,  
        },  
      
        mode:'card',  
      });  

style=”font-family: Arial; font-size: 1四px; line-height: ②陆px;”>看起来和TabNavigator很一般,一样是钦定了多少个参数:

style=”font-family: Arial; font-size: 1四px; line-height: 2陆px;”>(一)钦命要跳转的分界面组件。一样是screen属性标记分界面组件,不多废话。

style=”font-family: Arial; font-size: 1四px; line-height: 2陆px;”>(2)定义跳转属性参数,即顶部导航栏的部分参数设置和跳转格局。

style=”font-family: Arial; font-size: 1肆px; line-height: 二陆px;”>能够看看,大家将Tab作为二个分界面设置到了StackNavigator。那样就足以兑现Tab导航和分界面间跳转的功效了。

style=”font-family: Arial; font-size: 1四px; line-height: 二六px;”>最后正是在render中援引StackNavigator:

 

 export default class Demo extends Component {  
  
  render() {  
        return (  
          <Navigator />  
        );  
  }  
} 

 

style=”font-family: Arial; font-size: 14px; line-height: 二六px;”>StackNavigator还提供了onNavigationStateChange回调方法,用来监听导航状态的改造。具体不再赘言。完成了分界面跳转和切换,那么就该来扩展下分界面之间的心理了,来看看哪些达成分界面之间的传值和取值。

style=”font-family: Arial; font-size: 14px; line-height: 26px;”>5、分界面间跳转、传值、取值
在分界面组件注入到StackNavigator中时,分界面组件就被予以了navigation属性,即在分界面组件中能够通过【this.props.navigation】获取并开始展览局地操作。

navigation属性中提供了重重的函数简化分界面间操作,容易列举几点:

(一)通过navigate函数达成分界面之间跳转:

 

this.props.navigation.navigate('Mine');  

 

style=”font-family: Arial; font-size: 1肆px; line-height: 贰⑥px;”>参数为大家在StackNavigator注册分界面组件时的名目。一样也得以从眼下页面重回到上一页:

 

    // 返回上一页  
    this.props.navigation.goBack();  

 

style=”font-family: Arial; font-size: 1四px; line-height: 2陆px;”>(2)跳转时传值:

 

this.props.navigation.navigate('Mine',{info:'传值过去'});  

 

style=”font-family: Arial; font-size: 1四px; line-height: 二陆px;”> style=”font-family: Arial; font-size: 1四px; line-height: 2陆px;”>第贰个参数同样为要跳转的分界面组件名称,第四个参数为要传送的参数,info能够知晓为key,后边即传送的参数。

 

(3)获取值:

 

{this.props.navigation.state.params.info}  

 

style=”font-family: Arial; font-size: 1四px; line-height: 26px;”>通过state.params来博取传来的参数,前边为key值。此处为info。

style=”font-family: Arial; font-size: 1四px; line-height: 二6px;”>以上落成到位,我们就足以如沐春风的玩耍啦~~
什么?忽然开掘在Android上的功能和IOS效果不平等。首席推行官要分界面一致哇~
如何是好?那就须求大家实行简易的适配了。

 

三、DrawerNavigator完成抽屉导航

style=”font-family: Arial; font-size: 14px; line-height: 26px;”>
style=”font-family: Arial; font-size: 14px; line-height: 26px;”>壹、导航落成

style=”font-family: Arial; font-size: 14px; line-height: 26px;”>API定义:DrawerNavigator(RouteConfigs,DrawerNavigatorConfig)

style=”font-family: Arial; font-size: 1四px; line-height: 二六px;”>(壹)界面中定义DrawerNavigator:

    import {StackNavigator,TabNavigator,DrawerNavigator} from 'react-navigation';  
    import HomeScreen from './pages/HomePage';  
    import MineScreen from './pages/MinePage';  
      
    export default class Demo extends Component {  
      
      render() {  
            return (  
              <Navigator />  
            );  
      }  
    }  
      
    const Navigator = DrawerNavigator({  
      
        Home:{screen:HomeScreen},  
        Mine:{screen:MineScreen},  
    });  
      
    const styles = StyleSheet.create({  
      
        container: {  
            flex: 1,  
        },  
    });  
      
    AppRegistry.registerComponent('Demo', () => Demo);  

 

style=”font-family: Arial; font-size: 1肆px; line-height: 贰六px;”>定义格局和StackNavigator基本接近,不再赘言。

style=”font-family: Arial; font-size: 14px; line-height: 26px;”>(2)HomeScreen界面和MineScreen界面:

 

    export default class HomePage extends Component {  
      
        static navigationOptions = {  
            drawerLabel: '首页',  
            drawerIcon:({tintColor}) => (  
                <Image  
                    source={require('./../imgs/ic_happy.png')}  
                    style={[styles.icon, {tintColor: tintColor}]}/>  
            ),  
        };  
      
        render() {  
            return(  
                <View style={{flex:1}}>  
                    <Text onPress={this._skip.bind(this)}>点击跳转</Text>  
                </View>  
            );  
        }  
      
        _skip() {  
            this.props.navigation.navigate("Mine");  
        }  
    }  
      
      
    export default class MinePage extends Component {  
      
        static navigationOptions = {  
            drawerLabel:'我',  
             drawerIcon: ({ tintColor }) => (  
                <Image  
                    source={require('./../imgs/ic_h.png')}  
                    style={[styles.icon, {tintColor: tintColor}]}  
                />  
            ),  
        };  
      
        render() {  
            return(  
                <View style={{flex:1}}>  
                    <Text onPress={this._skip.bind(this)}>返回上一界面</Text>  
                </View>  
            );  
        }  
      
        /**  
         * 跳转  
         */  
        _skip() {  
            this.props.navigation.goBack();  
        }  
    }  

 

style=”font-family: Arial; font-size: 14px; line-height: 二陆px;”>代码很轻松,落成了分界面之间的跳转。

style=”font-family: Arial; font-size: 14px; line-height: 26px;”>2、扩充功效

style=”font-family: Arial; font-size: 1肆px; line-height: 二陆px;”>(壹)暗中认可DrawerView不可滚动。要兑现可滚动视图,必须选择contentComponent自定义容器,如下所示:

 

    {  
      drawerWidth:200,  
      抽屉位置:“对”  
      contentComponent:props => <ScrollView> <DrawerItems {... props} /> </ ScrollView>  
    }  

 

style=”font-family: Arial; font-size: 1肆px; line-height: 2陆px;”>(二)能够覆盖导航应用的默许组件,使用DrawerItems自定义导航组件:

 

    import {DrawerItems} from 'react-navigation';  
      
    const CustomDrawerContentComponent = (props) => (  
      <View style = {style.container}>  
        <DrawerItems {... props} />  
      </View>    
    );  

 

style=”font-family: Arial; font-size: 1四px; line-height: 贰陆px;”>(3)嵌套抽屉导航
style=”font-family: Arial; font-size: 1四px; line-height: 26px;”>就算你嵌套DrawerNavigation,抽屉将浮以后父导航下方。

 

4、react-navigation源码定制

style=”font-family: Arial; font-size: 14px; line-height: 26px;”>
style=”font-family: Arial; font-size: 1四px; line-height: 二六px;”>(一)适配顶部导航栏标题:

style=”font-family: Arial; font-size: 1四px; line-height: 26px;”> 测试中窥见,在iphone上标题栏的标题为居中状态,而在Android上则是居左对齐。所以必要大家修改源码,进行适配。

style=”font-family: Arial; font-size: 14px; line-height: 26px;”>【node_modules
— react-navigation — src — views —
Header.js】的326行代码处,修改为如下:

 

 title: {  
   bottom: 0,  
   left: TITLE_OFFSET,  
   right: TITLE_OFFSET,  
   top: 0,  
   position: 'absolute',  
   alignItems: 'center',  
 } 

style=”font-family: Arial; font-size: 14px; line-height: 二6px;”>(二)去除重回键文字展现:

style=”font-family: Arial; font-size: 14px; line-height: 26px;”>【node_modules
— react-navigation — src — views —
HeaderBackButton.js】的九1行代码处,修改为如下就能够。

 

 {Platform.OS === 'ios' &&  
     title &&  
     <Text  
       onLayout={this._onTextLayout}  
       style={[styles.title, { color: tintColor }]}  
       numberOfLines={1}  
     >  
       {backButtonTitle}  
     </Text>} 

 

style=”font-family: Arial; font-size: 14px; line-height: 二陆px;”>将上述代码删除就能够。

style=”font-family: Arial; font-size: 1四px; line-height: 二陆px;”>(三)设置标题栏的按键单击事件:

style=”font-family: Arial; font-size: 14px; line-height: 二陆px;”>因为在分界面组件中设置标题参数时,要求将navigationOptions定义成static。所以大家不可能直接通过this.xxx.bind(this)来调用自定义函数,如何做呢?能够通过怎么样格局缓和:

 

    class demo extends Component {  
      
        static navigationOptions =({navigation})=>({  
                right:( <Button  onPress={state.params.clickParams}/>)  
        })  
        _btnClick=()=> {  
         alert('单击')  
        };  
        componentWillMount() {  
           this.props.navigation.setParams({clickParams:this._btnClick})  
        }  
    }  

 

style=”font-family: Arial; font-size: 1四px; line-height: 26px;”>(四)动态设置标题栏展现和隐形

style=”font-family: Arial; font-size: 14px; line-height: 贰陆px;”>遵照(3)中的观念,大家能够应用setParams来动态修改状态栏的显得和藏身:

 

    class demo extends Component {  
        static navigationOptions = ({navigation}) =>({  
               visible: state.params.headerState ,  
        });  
      
        render(){  
            return(  
                <Button  
                title="Hide Header"  
                onPress={() => this.props.navigation.setParams({ headerState : 'none' })}  
            />)  
        }  
    }  

 

五、效果图

 

抽屉导航:

金沙国际 1

 

 

 

 



 

那篇小说真心值得珍藏!+点赞+分享!!!

 

小编介绍:半路学IT,做开荒三年,先下车在一家共享单车公司,做后台开荒!

 作者开了三个公众号,迎接各位有志同道合朋友,关怀!不定时分享职业,和本身得遗闻!

金沙国际 2

 

 

 

光阴戳格式

以此是最简便易行的也是最不易于出错的。当然唯一的败笔大致便是对开垦者不直观,不只怕1眼看出具体日子。
亟需专注的以下两点:

  1. js内的日子戳指的是眼后天子到1970年1月1日00:00:00 UTC对应的毫秒数,和unix时间戳不是1个概念,后者表示秒数,差了一千倍
  2. class=”crayon-syntax crayon-syntax-inline crayon-theme-github crayon-theme-github-inline crayon-font-monaco”
    style=”font-size: 13px !important; line-height: 15px !important;font-size: 13px !important;”> class=”crayon-pre crayon-code”
    style=”font-size: 13px !important; line-height: 15px !important;font-size: 13px !important; -moz-tab-size:四; -o-tab-size:四; -webkit-tab-size:四; tab-size:4;”> class=”crayon-r”>new class=”crayon-r”>Date( class=”crayon-v”>timestamp class=”crayon-sy”>) 中的时间戳必须是number格式,
    class=”crayon-syntax crayon-syntax-inline crayon-theme-github crayon-theme-github-inline crayon-font-monaco”
    style=”font-size: 13px !important; line-height: 15px !important;font-size: 13px !important;”> class=”crayon-pre crayon-code”
    style=”font-size: 13px !important; line-height: 15px !important;font-size: 13px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;”> class=”crayon-t”>string 会返回Invalid Date。所以比方new Date('11111111')那种写法是错的

(点击上方蓝字,快捷关心大家)

时光格式化函数的频率

这里的时间格式化值得是将时间字符串调换到微秒数的经过。js原生的光阴格式化函数有Date.parseDate.prototype.valueOfDate.prototype.getTimeNumber(Date)+Date(还有个Date.UTC艺术,然则对参数要求从严,不可能平素解析日期字符串,所以略过)
那多少个函数从功用上的话1模同样,但是现实的效用怎么着呢?作者写了个检查测试页面,诸位也能够协和测试下。
点击预览

 

总结

本文介绍了Python迭代器和生成器的连带内容。

看完本文有获得?请转载分享给更加多个人

关注「P**ython开发者」,提升Python技能**

金沙国际 3

总结

那篇小说断断续续地写了一个多月,然则由于对 Date() 函数的调节不足因而个人感觉其实依然思路有点乱,所以文章看起来只怕有个别有点跳跃性。但是用户本地化那块内容确实用了繁多主见去写,希望对看到那篇小说的人多少支持。

 

生成器

在Python中,使用生成器能够很有益于的援救迭代器协议。生成器通过生成器函数发生,生成器函数能够经过正规的def语句来定义,然而不用return重临,而是用yield一回回到2个结果,在每一种结果里面挂起和持续它们的情景,来自动完结迭代协议。

也正是说,yield是二个语法糖,内部贯彻补助了迭代器协议,同时yield内部是叁个状态机,维护着挂起和一而再的动静。

上面看看生成器的应用:

金沙国际 4

在这么些事例中,定义了三个生成器函数,函数再次回到一个生成器对象,然后就足以透过for语句进行迭代访问了。

实际上,生成器函数重临生成器的迭代器。
“生成器的迭代器”那么些术语平日被称作”生成器”。要小心的是生成器正是一类格外的迭代器。作为二个迭代器,生成器必必要定义一些艺术,个中多个就是next()。仿佛迭代器一样,我们得以应用next()函数来获取下二个值。

生成器实践流程

下边就仔细看看生成器是怎么职业的。

从上边的例子也得以见到,生成器函数跟平常的函数是有不小差别的。

整合地方的事例大家参与一些打字与印刷消息,进一步看看生成器的实施流程:

金沙国际 5

经过结果能够看出:

当调用生成器函数的时候,函数只是回来了多个生成器对象,并未试行。

当next()方法第二次被调用的时候,生成器函数才初叶奉行,实践到yield语句处结束

当继续调用next()方法的时候,函数将随即上二次结束的yield语句处继续实行,并到下1个yield处停止;假设前边未有yield就抛出StopIteration非凡

生成器表明式

在开班介绍生成器表明式以前,先看看大家相比熟知的列表解析( List
comprehensions),列表解析一般都以上面的款式。

style=”border-width: 0px;border-style: initial;border-color: initial;”>[ class=”crayon-e”
style=”border-width: 0px;border-style: initial;border-color: initial;”>expr
style=”border-width: 0px;border-style: initial;border-color: initial;”>for class=”crayon-h”
style=”border-width: 0px;border-style: initial;border-color: initial;”>
style=”border-width: 0px;border-style: initial;border-color: initial;”>iter_var
style=”border-width: 0px;border-style: initial;border-color: initial;”>in class=”crayon-h”
style=”border-width: 0px;border-style: initial;border-color: initial;”>
style=”border-width: 0px;border-style: initial;border-color: initial;”>iterable
style=”border-width: 0px;border-style: initial;border-color: initial;”>if class=”crayon-h”
style=”border-width: 0px;border-style: initial;border-color: initial;”>
style=”border-width: 0px;border-style: initial;border-color: initial;”>cond_expr class=”crayon-sy”
style=”border-width: 0px;border-style: initial;border-color: initial;”>]

迭代iterable里怀有内容,每趟迭代后,把iterable里满意cond_expr条件的始末放到iter_var中,再在表明式expr中应当iter_var的剧情,最终用表明式的测算值生成二个列表。

举例,生成三个list来敬重50以内的所以奇数:

style=”border-width: 0px;border-style: initial;border-color: initial;”>[ class=”crayon-i”
style=”border-width: 0px;border-style: initial;border-color: initial;”>i class=”crayon-h”
style=”border-width: 0px;border-style: initial;border-color: initial;”>
style=”border-width: 0px;border-style: initial;border-color: initial;”>for class=”crayon-h”
style=”border-width: 0px;border-style: initial;border-color: initial;”>
style=”border-width: 0px;border-style: initial;border-color: initial;”>i class=”crayon-h”
style=”border-width: 0px;border-style: initial;border-color: initial;”>
style=”border-width: 0px;border-style: initial;border-color: initial;”>in class=”crayon-h”
style=”border-width: 0px;border-style: initial;border-color: initial;”>
style=”border-width: 0px;border-style: initial;border-color: initial;”>range class=”crayon-sy”
style=”border-width: 0px;border-style: initial;border-color: initial;”>( class=”crayon-cn”
style=”border-width: 0px;border-style: initial;border-color: initial;”>50 class=”crayon-sy”
style=”border-width: 0px;border-style: initial;border-color: initial;”>) class=”crayon-h”
style=”border-width: 0px;border-style: initial;border-color: initial;”>
style=”border-width: 0px;border-style: initial;border-color: initial;”>if class=”crayon-h”
style=”border-width: 0px;border-style: initial;border-color: initial;”>
style=”border-width: 0px;border-style: initial;border-color: initial;”>i class=”crayon-o”
style=”border-width: 0px;border-style: initial;border-color: initial;”>% class=”crayon-cn”
style=”border-width: 0px;border-style: initial;border-color: initial;”>2 class=”crayon-sy”
style=”border-width: 0px;border-style: initial;border-color: initial;”>]

生成器表明式是在python二.四中引进的,当类别过长,
而每便只供给获得3个成分时,应当思考选择生成器表明式而不是列表解析。生成器表明式的语法和列表解析同样,只但是生成器说明式是被()括起来的,而不是[],如下:

class=”crayon-sy”
style=”border-width: 0px;border-style: initial;border-color: initial;”>( class=”crayon-e”
style=”border-width: 0px;border-style: initial;border-color: initial;”>expr
style=”border-width: 0px;border-style: initial;border-color: initial;”>for class=”crayon-h”
style=”border-width: 0px;border-style: initial;border-color: initial;”>
style=”border-width: 0px;border-style: initial;border-color: initial;”>iter_var
style=”border-width: 0px;border-style: initial;border-color: initial;”>in class=”crayon-h”
style=”border-width: 0px;border-style: initial;border-color: initial;”>
style=”border-width: 0px;border-style: initial;border-color: initial;”>iterable
style=”border-width: 0px;border-style: initial;border-color: initial;”>if class=”crayon-h”
style=”border-width: 0px;border-style: initial;border-color: initial;”>
style=”border-width: 0px;border-style: initial;border-color: initial;”>cond_expr class=”crayon-sy”
style=”border-width: 0px;border-style: initial;border-color: initial;”>)

看三个例证:

金沙国际 6

生成器表明式并不是创建1个列表,
而是重回贰个生成器,那几个生成器在历次总结出1个条条框框后,把这几个条目款项”爆发”(yield)出来。
生成器表达式使用了”惰性总结”(lazy
evaluation),唯有在检索时才被赋值(evaluated),所以在列表比较长的情形下选拔内部存款和储蓄器上更使得。

承接看多少个事例:

金沙国际 7

从那一个例子中得以见见,生成器表明式发生的生成器,它自己是3个可迭代对象,同时也是迭代器本人。

递归生成器

生成器能够向函数同样举办递归使用的,下边看贰个差不多的例证,对三个行列进行全排列:

style=”font-family: inherit;border-width: 0px;border-style: initial;border-color: initial;height: inherit;line-height: inherit !important;font-weight: bold !important;”>def class=”crayon-h”
style=”font-family: inherit;border-width: 0px;border-style: initial;border-color: initial;height: inherit;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>
style=”font-family: inherit;border-width: 0px;border-style: initial;border-color: initial;height: inherit;line-height: inherit !important;font-weight: inherit !important;color: teal !important;”>permutations class=”crayon-sy”
style=”font-family: inherit;border-width: 0px;border-style: initial;border-color: initial;height: inherit;line-height: inherit !important;font-weight: inherit !important;color: rgb(51, 51, 51) !important;”>( class=”crayon-v”
style=”font-family: inherit;border-width: 0px;border-style: initial;border-color: initial;height: inherit;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 45, 122) !important;”>li class=”crayon-sy”
style=”font-family: inherit;border-width: 0px;border-style: initial;border-color: initial;height: inherit;line-height: inherit !important;font-weight: inherit !important;color: rgb(51, 51, 51) !important;”>) class=”crayon-o”
style=”font-family: inherit;border-width: 0px;border-style: initial;border-color: initial;height: inherit;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>:

style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>     class=”crayon-st”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: bold !important;”>if class=”crayon-h”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: teal !important;”>len class=”crayon-sy”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(51, 51, 51) !important;”>( class=”crayon-v”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 45, 122) !important;”>li class=”crayon-sy”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(51, 51, 51) !important;”>) class=”crayon-h”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>
== style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 153, 153) !important;”>0 class=”crayon-o”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>:

style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>         class=”crayon-r”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: bold !important;”>yield class=”crayon-h”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: teal !important;”>li

style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: teal !important;”>     class=”crayon-st”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: bold !important;”>else class=”crayon-o”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>:

style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>         class=”crayon-st”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: bold !important;”>for class=”crayon-h”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;”>i class=”crayon-h”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: bold !important;”>in class=”crayon-h”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: teal !important;”>range class=”crayon-sy”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(51, 51, 51) !important;”>( class=”crayon-k”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: teal !important;”>len class=”crayon-sy”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(51, 51, 51) !important;”>( class=”crayon-v”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 45, 122) !important;”>li class=”crayon-sy”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(51, 51, 51) !important;”>)) class=”crayon-o”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>:

style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>             class=”crayon-v”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 45, 122) !important;”>li class=”crayon-sy”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(51, 51, 51) !important;”>[ class=”crayon-cn”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 153, 153) !important;”>0 class=”crayon-sy”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(51, 51, 51) !important;”>], class=”crayon-h”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 45, 122) !important;”>li class=”crayon-sy”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(51, 51, 51) !important;”>[ class=”crayon-v”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 45, 122) !important;”>i class=”crayon-sy”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(51, 51, 51) !important;”>] class=”crayon-h”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>
= style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 45, 122) !important;”>li class=”crayon-sy”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(51, 51, 51) !important;”>[ class=”crayon-v”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 45, 122) !important;”>i class=”crayon-sy”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(51, 51, 51) !important;”>], class=”crayon-h”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 45, 122) !important;”>li class=”crayon-sy”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(51, 51, 51) !important;”>[ class=”crayon-cn”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 153, 153) !important;”>0 class=”crayon-sy”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(51, 51, 51) !important;”>]

style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>             class=”crayon-st”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: bold !important;”>for class=”crayon-h”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: teal !important;”>item
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: bold !important;”>in class=”crayon-h”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: teal !important;”>permutations class=”crayon-sy”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(51, 51, 51) !important;”>( class=”crayon-v”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 45, 122) !important;”>li class=”crayon-sy”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(51, 51, 51) !important;”>[ class=”crayon-cn”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 153, 153) !important;”>1 class=”crayon-o”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>: class=”crayon-sy”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(51, 51, 51) !important;”>]) class=”crayon-o”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>:

style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>                 class=”crayon-r”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: bold !important;”>yield class=”crayon-h”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(51, 51, 51) !important;”>[ class=”crayon-v”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 45, 122) !important;”>li class=”crayon-sy”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(51, 51, 51) !important;”>[ class=”crayon-cn”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 153, 153) !important;”>0 class=”crayon-sy”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(51, 51, 51) !important;”>]] class=”crayon-h”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>

  • style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: teal !important;”>item

 

style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: bold !important;”>for class=”crayon-h”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: teal !important;”>item
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: bold !important;”>in class=”crayon-h”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: teal !important;”>permutations class=”crayon-sy”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(51, 51, 51) !important;”>( class=”crayon-k”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: teal !important;”>range class=”crayon-sy”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(51, 51, 51) !important;”>( class=”crayon-cn”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 153, 153) !important;”>3 class=”crayon-sy”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(51, 51, 51) !important;”>)) class=”crayon-o”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>:

style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>     class=”crayon-k”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: teal !important;”>print class=”crayon-h”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 45, 122) !important;”>item

代码的结果为:

金沙国际 8

生成器的send()和close()方法

生成器中还有五个很重大的秘诀:send()和close()。

从前方驾驭到,next()方法能够回复生成器状态并继续实行,其实send()是除next()外另1个过来生成器的法子。

Python
二.5中,yield语句产生了yield表明式,相当于说yield能够有3个值,而那几个值正是send()方法的参数,所以send(None)和next()是一样的。同样,next()和send()的再次来到值都以yield语句处的参数(yielded
value)

有关send()方法供给注意的是:调用send传入非None值前,生成器必须处于挂起状态,不然将抛出相当。也便是说,第2回调用时,要动用next()语句或send(None),因为从没yield语句来接收那个值。

那几个方法用于关闭生成器,对关闭的生成器后再也调用next或send将抛出StopIteration格外。

上面看看那多个点子的运用:

金沙国际 9

JS原生Date类型方法的一些冷知识

2015/09/07 · JavaScript
· Date

原稿出处:
chitanda   

三个多月没更新了-
-偷懒中。这么些东西其实很早此前就在料理了,可是新兴开掘自个儿不少地点没弄精晓,然后就直接卡那边了(其实正是不想写吧),想了下反正是给本人深谙js的原生API而已,所以也没须要太钻牛角尖,也不必然要多完整,由此就当是Date()函数的二个冷门知识点小补充吧。那篇文章主要讲Date()的字符串与时光戳调换以及用户时间本地化,恐怕内容上比较乱(不然也不会卡作者3个月时间了),见谅

ps:由于 Date() 是js原生函数,分歧浏览器的解析器对其得以落成格局并不一样,所以重临值也会有所差别。本文测试未尤其表明浏览器的情形下,均是指
win7 x6四+chrome 4四.0.240叁.155(正式版本) m (三十一位) 版本

 

style=”max-width: 100%;font-size: 14px;widows: 1;box-sizing: border-box !important;word-wrap: break-word !important;”>来源: style=”font-size: 1四px;”>田小布署 style=”color: rgb(四六, 四六, 四六);”>   style=”max-width: 百分之百;font-size: 14px;widows: 一;box-sizing: border-box !important;word-wrap: break-word !important;”>

style=”max-width: 100%;line-height: 25.6px;font-size: 14px;widows: 1;box-sizing: border-box !important;word-wrap: break-word !important;”>www.cnblogs.com/wilber2013/p/4652531.html

选择js获取用户浏览器语言喜好

对于获得那三种设置,js原生方法扶助度都比较一般:
IE下的 navigator 方法有多样和language有关的艺术,分化如下:
假设系统语言为  ja-JP ,系统unicode语言为 zh-CN 日期格式为nl-NL,浏览器语言设置(accept-language)为
de ,浏览器分界面语言为 en-US (别的规格不改变,浏览器分界面语言改为
zh-CN 的时候结果也是一模同样),

JavaScript

window.navigator.language //”nl-NL” window.navigator.systemLanguage
//”zh-CN”(设置中的非unicode程序所利用语言选用)
window.navigator.userLanguage //”nl-NL” window.navigator.browserLanguage
//”ja-JP”(系统菜单分界面语言) window.navigator.languages //undefined

1
2
3
4
5
6
7
8
9
10
window.navigator.language
//"nl-NL"
window.navigator.systemLanguage
//"zh-CN"(设置中的非unicode程序所使用语言选项)
window.navigator.userLanguage
//"nl-NL"
window.navigator.browserLanguage
//"ja-JP"(系统菜单界面语言)
window.navigator.languages
//undefined

chrome下,当浏览器分界面语言为 zh-CN, accept-language首位为 en-US 的时候:

JavaScript

window.navigator.language //’zh-CN’ window.navigator.languages
//[“en-US”, “en”, “zh-CN”, “zh”, “ja”, “zh-TW”, “de-LI”, “de”, “pl”]
//当分界面语言改为”en-US”时 window.navigator.language
//’en-US’(浏览器分界面语言)

1
2
3
4
5
6
7
window.navigator.language
//’zh-CN’
window.navigator.languages
//["en-US", "en", "zh-CN", "zh", "ja", "zh-TW", "de-LI", "de", "pl"]
//当界面语言改为"en-US"时
window.navigator.language
//’en-US’(浏览器界面语言)

FF下,当浏览器分界面语言为 zh-CN ,accept-language首位为 en-US 的时候:

JavaScript

window.navigator.language //’en-US’ window.navigator.languages
//[“en-US”, “zh-CN”, “de”, “zh”, “en”]
//当界面语言改为”en-US”,`accept-language`首位为`zh-CN`的时候
window.navigator.language //’zh-CN’(`accept-language`首选值)
window.navigator.languages //[“zh-CN”, “de”, “zh”, “en-US”, “en”]

1
2
3
4
5
6
7
8
9
window.navigator.language
//’en-US’
window.navigator.languages
//["en-US", "zh-CN", "de", "zh", "en"]
//当界面语言改为"en-US",`accept-language`首位为`zh-CN`的时候
window.navigator.language
//’zh-CN’(`accept-language`首选值)
window.navigator.languages
//["zh-CN", "de", "zh", "en-US", "en"]
  1. 从上边的测试结果可以很分明的开采IE浏览器的那多少个函数都是获得系统新闻的,不可能取获得前边提到的四个浏览器层面上的装置。(那多少个函数具体意思还格外的能够参考MSDN官方文书档案)
  2. class=”crayon-syntax crayon-syntax-inline crayon-theme-github crayon-theme-github-inline crayon-font-monaco”
    style=”font-size: 13px !important; line-height: 15px !important;font-size: 13px !important;”> class=”crayon-pre crayon-code”
    style=”font-size: 13px !important; line-height: 15px !important;font-size: 13px !important; -moz-tab-size:4; -o-tab-size:四; -webkit-tab-size:肆; tab-size:四;”> class=”crayon-v”>window class=”crayon-sy”>. class=”crayon-v”>navigator class=”crayon-sy”>. class=”crayon-v”>language 这一个函数即使四个浏览器都得以合营,然则代表的含义完全两样。IE下该函数再次回到系统安装的年华展现格式所服从的科班的地段代码;chrome下回到浏览器界面语言;FF下重临accept-language的首荐语言值

由此:

  1. 浏览器设置的语言accept-language值,IE浏览器无法使用JS获取。chrome和FF浏览器都足以应用
     class="crayon-syntax crayon-syntax-inline crayon-theme-github crayon-theme-github-inline crayon-font-monaco"
    

    style=”font-size: 13px !important; line-height: 15px !important;font-size: 13px !important;”> class=”crayon-pre crayon-code”
    style=”font-size: 13px !important; line-height: 15px !important;font-size: 13px !important; -moz-tab-size:四; -o-tab-size:4; -webkit-tab-size:四; tab-size:四;”> class=”crayon-v”>window class=”crayon-sy”>. class=”crayon-v”>navigator class=”crayon-sy”>. class=”crayon-v”>languages 来获取,而FF仍是可以直接用 

     class="crayon-syntax crayon-syntax-inline crayon-theme-github crayon-theme-github-inline crayon-font-monaco"
    

    style=”font-size: 13px !important; line-height: 15px !important;font-size: 13px !important;”> class=”crayon-pre crayon-code”
    style=”font-size: 13px !important; line-height: 15px !important;font-size: 13px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:四; tab-size:4;”> class=”crayon-v”>window class=”crayon-sy”>. class=”crayon-v”>navigator class=”crayon-sy”>. class=”crayon-v”>language 直接拿走accept-language的首要推荐语言值。所以对于accept-language,兼容性最佳的拿走形式应该是行使后端,发起3个ajax请求,分析header。而不是一直js来管理。

  2. 浏览器界面语言,IE和FF都没办法儿选取js来赢得,chrome能够用 id=”crayon-五b8f陆c187陆柒b325378298陆”
    class=”crayon-syntax crayon-syntax-inline crayon-theme-github crayon-theme-github-inline crayon-font-monaco”
    style=”font-size: 1三px !important; line-height: 15px !important;font-size: 13px !important;”> class=”crayon-pre crayon-code”
    style=”font-size: 13px !important; line-height: 15px !important;font-size: 13px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;”> class=”crayon-v”>window class=”crayon-sy”>. class=”crayon-v”>navigator class=”crayon-sy”>. class=”crayon-v”>language 来获取
  3. 系统级其他言语设置(系统菜单分界面语言,系统安装的光阴展现格式),chrome和FF都没办法儿用JS获取到

 

迭代器

迭代器对象要求援助迭代器协议的目的,在Python中,帮忙迭代器协议正是贯彻目的的__iter__()和next()方法。其中__iter__()方法重回迭代器对象自己;next()方法重返容器的下二个因素,在结尾时引发StopIteration至极。

__iter__()和next()方法

那七个艺术是迭代器最基本的措施,三个用来赢得迭代器对象,二个用来获取容器中的下3个因素。

对于可迭代对象,能够运用内建函数iter()来获得它的迭代器对象:

金沙国际 10

事例中,通过iter()方法得到了list的迭代器对象,然后就能够透过next()方法来拜会list中的成分了。当容器中从不可访问的成分后,next()方法将会抛出二个StopIteration分外终止迭代器。

其实,当大家应用for语句的时候,for语句就会活动的通过__iter__()方法来得到迭代器对象,并且经过next()方法来赢得下三个因素。

自定义迭代器

打探了迭代器协议之后,就能够自定义迭代器了。

上面例子中贯彻了一个MyRange的种类,那几个类型中贯彻了__iter__()方法,通过那一个办法重返对象自己作为迭代器对象;同时,完成了next()方法用来赢得容器中的下四个成分,当未有可访问成分后,就抛出StopIteration极度。

style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;line-height: inherit !important;font-weight: bold !important;color: rgb(128, 0, 128) !important;”>class class=”crayon-h”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;line-height: inherit !important;font-weight: inherit !important;color: teal !important;”>MyRange class=”crayon-sy”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;line-height: inherit !important;font-weight: inherit !important;color: rgb(51, 51, 51) !important;”>( class=”crayon-k”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;line-height: inherit !important;font-weight: inherit !important;color: teal !important;”>object class=”crayon-sy”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;line-height: inherit !important;font-weight: inherit !important;color: rgb(51, 51, 51) !important;”>) class=”crayon-o”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>:

style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>     class=”crayon-r”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: bold !important;”>def class=”crayon-h”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: teal !important;”>__init__ class=”crayon-sy”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(51, 51, 51) !important;”>( class=”crayon-r”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: bold !important;”>self class=”crayon-sy”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(51, 51, 51) !important;”>, class=”crayon-h”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 45, 122) !important;”>n class=”crayon-sy”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(51, 51, 51) !important;”>) class=”crayon-o”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>:

style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>         class=”crayon-r”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: bold !important;”>self class=”crayon-sy”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(51, 51, 51) !important;”>. class=”crayon-v”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 45, 122) !important;”>idx class=”crayon-h”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>
= style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 153, 153) !important;”>0

style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>         class=”crayon-r”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: bold !important;”>self class=”crayon-sy”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(51, 51, 51) !important;”>. class=”crayon-v”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 45, 122) !important;”>n class=”crayon-h”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>
= style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;”>n

 

style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>     class=”crayon-r”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: bold !important;”>def class=”crayon-h”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: teal !important;”>__iter__ class=”crayon-sy”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(51, 51, 51) !important;”>( class=”crayon-r”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: bold !important;”>self class=”crayon-sy”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(51, 51, 51) !important;”>) class=”crayon-o”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>:

style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>         class=”crayon-st”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: bold !important;”>return class=”crayon-h”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: bold !important;”>self

 

style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>     class=”crayon-r”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: bold !important;”>def class=”crayon-h”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: teal !important;”>next class=”crayon-sy”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(51, 51, 51) !important;”>( class=”crayon-r”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: bold !important;”>self class=”crayon-sy”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(51, 51, 51) !important;”>) class=”crayon-o”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>:

style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>         class=”crayon-st”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: bold !important;”>if class=”crayon-h”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: bold !important;”>self class=”crayon-sy”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(51, 51, 51) !important;”>. class=”crayon-v”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 45, 122) !important;”>idx class=”crayon-h”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>
< style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: bold !important;”>self class=”crayon-sy”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(51, 51, 51) !important;”>. class=”crayon-v”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 45, 122) !important;”>n class=”crayon-o”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>:

style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>             class=”crayon-v”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 45, 122) !important;”>val class=”crayon-h”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>
= style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: bold !important;”>self class=”crayon-sy”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(51, 51, 51) !important;”>. class=”crayon-e”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: teal !important;”>idx

style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: teal !important;”>             class=”crayon-r”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: bold !important;”>self class=”crayon-sy”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(51, 51, 51) !important;”>. class=”crayon-v”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 45, 122) !important;”>idx class=”crayon-h”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>
+= style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 153, 153) !important;”>1

style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>             class=”crayon-st”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: bold !important;”>return class=”crayon-h”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: teal !important;”>val

style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: teal !important;”>         class=”crayon-st”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: bold !important;”>else class=”crayon-o”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>:

style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>             class=”crayon-st”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: bold !important;”>raise class=”crayon-h”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: teal !important;”>StopIteration class=”crayon-sy”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(51, 51, 51) !important;”>()

那个自定义类型跟内建函数xrange很接近,看一下运维结果:

style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 45, 122) !important;”>myRange class=”crayon-h”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>
= style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;line-height: inherit !important;font-weight: inherit !important;color: teal !important;”>MyRange class=”crayon-sy”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;line-height: inherit !important;font-weight: inherit !important;color: rgb(51, 51, 51) !important;”>( class=”crayon-cn”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 153, 153) !important;”>3 class=”crayon-sy”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;line-height: inherit !important;font-weight: inherit !important;color: rgb(51, 51, 51) !important;”>)

style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: bold !important;”>for class=”crayon-h”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;”>i class=”crayon-h”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: bold !important;”>in class=”crayon-h”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 45, 122) !important;”>myRange class=”crayon-o”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>:

style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: teal !important;”>print class=”crayon-h”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 45, 122) !important;”>i

金沙国际 11

迭代器和可迭代对象

在上边的事例中,myRange那么些目的正是一个可迭代对象,同时它本身也是三个迭代器对象。

看上边包车型地铁代码,对于一个可迭代对象,假若它自个儿又是1个迭代器对象,就会有下边包车型地铁难点,就从不章程扶助多次迭代。

金沙国际 12

金沙国际,为了减轻地点的主题材料,能够分别定义可迭代类型对象和迭代器类型对象;然后可迭代类型对象的__iter__()方法能够收获二个迭代器类型的目的。看上边包车型地铁得以达成:

style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;line-height: inherit !important;font-weight: bold !important;color: rgb(128, 0, 128) !important;”>class class=”crayon-h”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 45, 122) !important;”>Zrange class=”crayon-o”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>:

style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>     class=”crayon-r”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: bold !important;”>def class=”crayon-h”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: teal !important;”>__init__ class=”crayon-sy”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(51, 51, 51) !important;”>( class=”crayon-r”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: bold !important;”>self class=”crayon-sy”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(51, 51, 51) !important;”>, class=”crayon-h”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 45, 122) !important;”>n class=”crayon-sy”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(51, 51, 51) !important;”>) class=”crayon-o”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>:

style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>         class=”crayon-r”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: bold !important;”>self class=”crayon-sy”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(51, 51, 51) !important;”>. class=”crayon-v”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 45, 122) !important;”>n class=”crayon-h”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>
= style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;”>n

 

style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>     class=”crayon-r”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: bold !important;”>def class=”crayon-h”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: teal !important;”>__iter__ class=”crayon-sy”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(51, 51, 51) !important;”>( class=”crayon-r”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: bold !important;”>self class=”crayon-sy”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(51, 51, 51) !important;”>) class=”crayon-o”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>:

style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>         class=”crayon-st”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: bold !important;”>return class=”crayon-h”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: teal !important;”>ZrangeIterator class=”crayon-sy”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(51, 51, 51) !important;”>( class=”crayon-r”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: bold !important;”>self class=”crayon-sy”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(51, 51, 51) !important;”>. class=”crayon-v”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 45, 122) !important;”>n class=”crayon-sy”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(51, 51, 51) !important;”>)

 

style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: bold !important;color: rgb(128, 0, 128) !important;”>class class=”crayon-h”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 45, 122) !important;”>ZrangeIterator class=”crayon-o”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>:

style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>     class=”crayon-r”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: bold !important;”>def class=”crayon-h”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: teal !important;”>__init__ class=”crayon-sy”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(51, 51, 51) !important;”>( class=”crayon-r”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: bold !important;”>self class=”crayon-sy”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(51, 51, 51) !important;”>, class=”crayon-h”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 45, 122) !important;”>n class=”crayon-sy”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(51, 51, 51) !important;”>) class=”crayon-o”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>:

style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>         class=”crayon-r”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: bold !important;”>self class=”crayon-sy”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(51, 51, 51) !important;”>. class=”crayon-v”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 45, 122) !important;”>i class=”crayon-h”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>
= style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 153, 153) !important;”>0

style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>         class=”crayon-r”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: bold !important;”>self class=”crayon-sy”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(51, 51, 51) !important;”>. class=”crayon-v”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 45, 122) !important;”>n class=”crayon-h”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>
= style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;”>n

 

style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>     class=”crayon-r”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: bold !important;”>def class=”crayon-h”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: teal !important;”>__iter__ class=”crayon-sy”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(51, 51, 51) !important;”>( class=”crayon-r”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: bold !important;”>self class=”crayon-sy”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(51, 51, 51) !important;”>) class=”crayon-o”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>:

style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>         class=”crayon-st”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: bold !important;”>return class=”crayon-h”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: bold !important;”>self

 

style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>     class=”crayon-r”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: bold !important;”>def class=”crayon-h”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: teal !important;”>next class=”crayon-sy”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(51, 51, 51) !important;”>( class=”crayon-r”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: bold !important;”>self class=”crayon-sy”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(51, 51, 51) !important;”>) class=”crayon-o”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>:

style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>         class=”crayon-st”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: bold !important;”>if class=”crayon-h”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: bold !important;”>self class=”crayon-sy”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(51, 51, 51) !important;”>. class=”crayon-v”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 45, 122) !important;”>i class=”crayon-h”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>
< style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: bold !important;”>self class=”crayon-sy”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(51, 51, 51) !important;”>. class=”crayon-v”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 45, 122) !important;”>n class=”crayon-o”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>:

style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>             class=”crayon-v”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 45, 122) !important;”>i class=”crayon-h”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>
= style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: bold !important;”>self class=”crayon-sy”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(51, 51, 51) !important;”>. class=”crayon-i”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;”>i

style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>             class=”crayon-r”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: bold !important;”>self class=”crayon-sy”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(51, 51, 51) !important;”>. class=”crayon-v”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 45, 122) !important;”>i class=”crayon-h”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>
+= style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 153, 153) !important;”>1

style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>             class=”crayon-st”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: bold !important;”>return class=”crayon-h”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;”>i

style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>         class=”crayon-st”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: bold !important;”>else class=”crayon-o”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>:

style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>             class=”crayon-st”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: bold !important;”>raise class=”crayon-h”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: teal !important;”>StopIteration class=”crayon-sy”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(51, 51, 51) !important;”>() class=”crayon-h”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>    

 

style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 45, 122) !important;”>zrange class=”crayon-h”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>
= style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: teal !important;”>Zrange class=”crayon-sy”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(51, 51, 51) !important;”>( class=”crayon-cn”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 153, 153) !important;”>3 class=”crayon-sy”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(51, 51, 51) !important;”>)

style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: teal !important;”>print class=”crayon-h”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: teal !important;”>zrange
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: bold !important;”>is class=”crayon-h”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: teal !important;”>iter class=”crayon-sy”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(51, 51, 51) !important;”>( class=”crayon-v”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 45, 122) !important;”>zrange class=”crayon-sy”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(51, 51, 51) !important;”>) class=”crayon-h”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>        

 

style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: teal !important;”>print class=”crayon-h”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(51, 51, 51) !important;”>[ class=”crayon-i”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;”>i class=”crayon-h”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: bold !important;”>for class=”crayon-h”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;”>i class=”crayon-h”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: bold !important;”>in class=”crayon-h”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 45, 122) !important;”>zrange class=”crayon-sy”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(51, 51, 51) !important;”>]

style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: teal !important;”>print class=”crayon-h”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(51, 51, 51) !important;”>[ class=”crayon-i”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;”>i class=”crayon-h”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: bold !important;”>for class=”crayon-h”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;”>i class=”crayon-h”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: bold !important;”>in class=”crayon-h”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 111, 224) !important;”>
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(0, 45, 122) !important;”>zrange class=”crayon-sy”
style=”border-width: 0px;border-style: initial;border-color: initial;font-family: inherit;height: inherit;font-size: inherit !important;line-height: inherit !important;font-weight: inherit !important;color: rgb(51, 51, 51) !important;”>]

代码的运作结果为:

金沙国际 13

实质上,通过下边代码能够看到,list类型也是根据下边的方法,list本人是3个可迭代对象,通过iter()方法能够获得list的迭代器对象:

金沙国际 14

格林威治规范时间GMT

土霉素T即「格林威治标准时间」(格林wich Mean
Time,简称G.M.T.),指位于U.K.London天长市的皇家格林威治天文台的行业内部时间,因为本初子午线被定义为经过那里的经线。可是由于地球的狼狈自转,导致博来霉素T时间有引用误差,由此近期已不被用作规范时间使用。

new Date()解析所帮助的参数格式规范

实质上,那一经过进行的老大快。尽管服务器的管理速度极慢也可经常地施行。其它,由于请求是异步的,所以能够而且管理三个请求

Date()与new Date()的区别

Date() 直接再次来到当前时光字符串,不论参数是number照旧别的string

JavaScript

Date(); Date(‘sssss’); Date(1000); //Fri Aug 二一 20壹伍 一5:四六:21 核糖霉素T+0800
(中华夏族民共和国家规范准时间)

1
2
3
4
Date();
Date(‘sssss’);
Date(1000);
//Fri Aug 21 2015 15:46:21 GMT+0800 (中国标准时间)

而 new Date() 则是会依据参数来回到对应的值,无参数的时候,再次来到当前岁月的字符串情势;有参数的时候回来参数所对应时间的字符串。
new Date() 对参数不管是格式如故内容都需求,且只回去字符串,

JavaScript

new Date(); //Fri Aug 21 201五 15:5壹:5五 地霉素T+0800 (中中原人民共和国标准时间) new
Date(129387玖陆仟00); new Date(‘201一-0一-0一T11:00:00’) new
Date(‘二零一一/01/0壹 1一:00:00’) new Date(201一,0,一,1一,0,0) new Date(‘jan 01
201一,1一 1一:00:00’) new Date(‘Sat Jan 0一 201一 11:00:00’) //Sat Jan 01
201壹 11:00:00 威他霉素T+0800 (中国家规范准时间) new Date(‘sss’); new
Date(‘二〇一三/01/0壹T1壹:00:00’); new Date(‘201一-01-01-1一:00:00’) new
Date(‘129387玖伍仟00’); //Invalid Date new
Date(‘201一-0一-0一T1一:00:00’)-new Date(‘一9玖4/02/1一 1二:00:12’)
//59606998捌仟

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
new Date();
//Fri Aug 21 2015 15:51:55 GMT+0800 (中国标准时间)
 
new Date(1293879600000);
new Date(‘2011-01-01T11:00:00’)
new Date(‘2011/01/01 11:00:00’)
new Date(2011,0,1,11,0,0)
new Date(‘jan 01 2011,11 11:00:00’)
new Date(‘Sat Jan 01 2011 11:00:00’)
//Sat Jan 01 2011 11:00:00 GMT+0800 (中国标准时间)
 
new Date(‘sss’);
new Date(‘2011/01/01T11:00:00’);
new Date(‘2011-01-01-11:00:00’)
new Date(‘1293879600000’);
//Invalid Date
 
new Date(‘2011-01-01T11:00:00’)-new Date(‘1992/02/11 12:00:12’)
//596069988000

从上边多少个测试结果能够很轻松发掘

  1. new Date() 在参数平日的事态只会回去当前时刻的字符串(且是时下时区的年月)
  2. new Date() 在解析2个切实可行的时刻的时候,对参数有较严厉的格式供给,格式不得法的时候会平素回到Invalid Date,比方将
    number 类的年华戳调换来 string 类的时候也会招致解析出错
  3. 即便 new Date() 的再次来到值是字符串,不过三个new Date() 的结果字符串是足以直接相减的,结果为距离的纳秒数。

那就是说, new Date() 能接受的参数格式到底是何许规范呢?(相对于严厉须求的多参数传值方法。非严俊的单参数(数字日期表示格式)更常用且更易于失误,所以下文只思量单参数数字时间字符串调换的情况)

意味着格式)更常用且更易于失误,所以下文只考虑单参数数字时间字符串转变的境况)


 

浏览器分界面语言设置和语言设置的区分

那小节貌似有点跑题,然则不表达下的很轻巧和方面提到的浏览器设置的言语混淆视听,所以也拿出的话一下。
内需留意浏览器的语言设置和界面语言设置不是一回事
浏览器的语言设置设置的是浏览器发送给服务器的Request Header里的Accept-Language的值,那一个值能够告诉服务器用户的喜好语言,对于一些跨国网址,服务器可以以此为仍旧来回到对应语言的页面(不超过实际在使用上这些范围相当大,超越四陆%网址依旧基于IP来判定用户来源的,大概直接让用户自个儿采纳)
对此各大浏览器来讲,这一个装置的改变也是比较显性,轻便找到的。
IE: Internet选项语言
FF: 选项内容语言
chrome:设置显示高级设置语言语言和输入设置...
上边那里的安装不会潜移默化到浏览器的分界面语言设置,以国内很多用户来说,即无论你怎么设置那里的言语选取,浏览器菜单等私下认可都会是以中文展现的.
浏览器的界面语言设置诚如的话则藏的深得多,没那么轻巧找到。
IE:
卸载前边安装过的浏览器语言包,去微软官方网址下载对应的IE浏览器语言包安装。(和装置的语言包有关。系统分界面语言和该语言包相同的情事下,变为该语言。不然以安装的语言包为准。)
FF:地址栏输入about:config,然后找到general.useragent.locale字段,修改对应字段就可以。
chrome:设置显示高级设置语言语言和输入设置...

JavaScript 中 Date 对象的各类创立格式示例:

birthday = new Day(); //不指定,其值为 new 对象时的时间

birthday = new Day("October 9,2016 23:03:00"); //通过字符串参数指定年月日时分秒

birthday = new Day(10, 9, 2016, 23, 3, 0);//通过数字参数指定年月日时分秒

birthday = new Day(10, 9, 2016);//通过数字参数指定年月日

getYear() 再次回到值为两位数的年份(如“16”),getFullYear()
再次回到值为2人数的年度(如“201六”),使用getFullYear() 可制止”三千年”难点。

参考文献

  1. Date and Time Formats
  2. Date and Time
    Specification(RFC2822)
  3. Date.parse()-Differences in assumed time
    zone
  4. JavaScript and Dates, What a
    Mess!
  5. navigator
    object(IE浏览器私有language函数的分析)

    1 赞 收藏
    评论

金沙国际 15

浏览器获取用户眼下岁月以及喜好语言

首先须求注意一点,浏览器获取当前用户所在的时区等消息只和种类的日期和时间设置里的时区以及时光关于。区域和语言安装影响的是浏览器私下认可时间函数(Date.prototype.toLocaleString等)展现的格式,不会对时区等有影响。以window为例,控制面板\时钟、语言和区域中的多少个子设置项目标界别如下:

日期和时间:设置当前用户所处的时刻和时区,浏览器获取到的结果以此为准,哪怕用户的装置时间和时区是完全错误的。比方若东捌区的用户将和谐的时区设置为东9区,浏览器就会将视为东九区;时间数额上同理。那里的装置会潜移默化Date.prototype.getTimezoneOffsetnew Date()的值

区域和语言:首若是安装系统暗中认可的岁月展现格局。其子设置的格式会影响Date.prototype.toLocaleString艺术重临的字符串结果

 

测试结果:

单次实施50W次时间格式化函数,天公地道复测试九十八次,最后的结果如下:
(表格中的数字为单次施行50W次函数的平均结果。单位为微秒)

函数 chrome IE Firefox
Date.parse() 151.2087 55.5811 315.0446
Date.prototype.getTime() 19.5452 21.3423 14.0169
Date.prototype.valueOf() 20.1696 21.7192 13.8096
+Date() 20.0044 31.3511 22.7861
Number(Date) 23.0900 24.8838 23.3775

从那些表格能够很轻便得出以下结论:

  1. 从计算作用上的话,Date.prototype.getTime()Date.prototype.valueOf()>+DateNumber(Date)>>Date.parse()
  2. 从代码书写效能上的话,对于一点点的年华格式化总计,用+Date()或者Number(Date)就可以。而若页面内有雅量该管理,则建议用Date原生的函数Date.prototype.getTime()或者Date.prototype.valueOf().只有Date.parse,找不到别的利用的说辞。
  3. 那一个结果和计算机的测算性能以及浏览器有关,所以实际数字或然会有异常的大偏差,很正规。然则多少个函数结果的命宫差大小顺序并不会变。
  4. codepen的在线demo限制非常的大,对于这一个考试个人提出最棒将源代码复制到当三步跳件然后开始展览测试

 

世界和睦时间UTC

UTC是最根本的社会风气时间标准,是透过平分太阳时(以格林威治时间卡那霉素T为准)、地轴运动改正后的新时标以及以「秒」为单位的国际原牛时所综合计算而成的日子。UTC比核糖霉素T来得更精准。其固有误差值必须维持在0.九秒之内,若高于0.九秒则由位于香水之都的国际地球自转事务中心局发表闰秒,使UTC与地球自转周期一致。而是普通行使中,创新霉素T与UTC的效用与正确度是不曾距离的。
和睦世界时区会接纳“Z”来代表。而在航空上,全部应用的小运划一显明是和煦世界时。而且Z在有线电中应读作“Zulu”(可参见北约音标字母),和谐世界时也会被称作“Zulu
time”。

浏览器推断用户当地字符串格式

Date有个 Date.prototype.toLocaleString() 方法可以将时刻字符串再次来到用户本地字符串格式,那几个艺术还有多少个子方法
Date.prototype.toLocaleDateString 和 Date.prototype.toLocaleTimeString ,那多个方法重临值分别表示日期时间,加一齐就是Date.prototype.toLocaleString 的结果。
那个方法的暗许参数会对时间字符串做一遍转换,将其转变到用户日前所在时区的岁月,并依据相应的体系安装时间格式重回字符串结果。但是不等浏览器对用户本地所选取的语言格式的论断依附是例外的。
IE:获取系统当下的区域和语言格式中装置的格式,依照其对应的格式来显示当后天子结果;IE浏览器实时查询该种类设置(即你在浏览器窗口展开后去改造系统设置也会挑起重返格式变化)
FF:获取情势和结果与IE浏览器同样,差距在于FF只会在浏览器进度第一回开发银行的时候获得二遍系统安装,中间不管怎么系统安装怎么变化,FF都不可能获得到方今系统设置。除非重启FF浏览器。
Chrome:获取格局和上述八个都比不上。chrome无视系统的区域和语言格式格式,只依据本人浏览器的分界面设置的菜单语言来拍卖。(比如英文分界面则按系统’en-US’格式再次来到字符串,汉语分界面则按系统’zh-CN’格式重返结果)
不问可见:

chrome下浏览器语言设置优先系统语言设置。而IE和FF则是系统语言设置优先浏览器语言设置,不管浏览器分界面语言是怎么样,他们只根据系统装置来回到格式。(未有MAC,所以不晓得safari是啥情状,等随后看情况补充吧)
除此以外,分裂浏览器对toLocaleString归来的结果也是例外的,IE浏览器严峻遵守系统安装,而chrome和FF会有本身放开的格式来替换。

光阴数字字符串格式

一点都不大清楚那种该怎么描述,就是接近YYYY/MM/DD HH:mm:SS这种。下文以dateString代指。
new Date(dateString)所协理的字符串格式供给满意RFC2822标准或者ISO
8601标准
那三种规范对应的格式分别如下:

  1. PAJEROFC282贰 标准日期字符串
JavaScript

YYYY/MM/DD HH:MM:SS ± timezon(时区用4位数字表示) // eg 1992/02/12
12:23:22+0800

<table>
<colgroup>
<col style="width: 50%" />
<col style="width: 50%" />
</colgroup>
<tbody>
<tr class="odd">
<td><div class="crayon-nums-content" style="font-size: 13px !important; line-height: 15px !important;">
<div class="crayon-num" data-line="crayon-5b8f6c187675a314957670-1">
1
</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-5b8f6c187675a314957670-2">
2
</div>
</div></td>
<td><div class="crayon-pre" style="font-size: 13px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
<div id="crayon-5b8f6c187675a314957670-1" class="crayon-line">
YYYY/MM/DD HH:MM:SS ± timezon(时区用4位数字表示)
</div>
<div id="crayon-5b8f6c187675a314957670-2" class="crayon-line crayon-striped-line">
// eg 1992/02/12 12:23:22+0800
</div>
</div></td>
</tr>
</tbody>
</table>

>  RFC2822还有别的格式,不过上面这个是比较常用的(另外这标准太难啃了,实在没耐心啃完,所以也就没太深入)。RFC2822标准本身还有其他的非数字日期表达方式,不过不在这个话题讨论范围内了,略过
  1. ISO 8601职业日期字符串
JavaScript

YYYY-MM-DDThh:mm:ss ± timezone(时区用HH:MM表示) 1997-07-16T08:20:30Z
//
“Z”表示UTC标准时区,即"00:00",所以这里表示零时区的\`1997年7月16日08时20分30秒\`
//转换成位于东八区的北京时间则为\`1997年7月17日16时20分30秒\`
1997-07-16T19:20:30+01:00 //
表示东一区的1997年7月16日19时20秒30分,转换成UTC标准时间的话是1997-07-16T18:20:30Z

<table>
<colgroup>
<col style="width: 50%" />
<col style="width: 50%" />
</colgroup>
<tbody>
<tr class="odd">
<td><div class="crayon-nums-content" style="font-size: 13px !important; line-height: 15px !important;">
<div class="crayon-num" data-line="crayon-5b8f6c187675d765819674-1">
1
</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-5b8f6c187675d765819674-2">
2
</div>
<div class="crayon-num" data-line="crayon-5b8f6c187675d765819674-3">
3
</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-5b8f6c187675d765819674-4">
4
</div>
<div class="crayon-num" data-line="crayon-5b8f6c187675d765819674-5">
5
</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-5b8f6c187675d765819674-6">
6
</div>
<div class="crayon-num" data-line="crayon-5b8f6c187675d765819674-7">
7
</div>
<div class="crayon-num crayon-striped-num" data-line="crayon-5b8f6c187675d765819674-8">
8
</div>
</div></td>
<td><div class="crayon-pre" style="font-size: 13px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
<div id="crayon-5b8f6c187675d765819674-1" class="crayon-line">
 YYYY-MM-DDThh:mm:ss ± timezone(时区用HH:MM表示)
</div>
<div id="crayon-5b8f6c187675d765819674-2" class="crayon-line crayon-striped-line">
 
</div>
<div id="crayon-5b8f6c187675d765819674-3" class="crayon-line">
 1997-07-16T08:20:30Z
</div>
<div id="crayon-5b8f6c187675d765819674-4" class="crayon-line crayon-striped-line">
 // “Z”表示UTC标准时区,即&quot;00:00&quot;,所以这里表示零时区的`1997年7月16日08时20分30秒`
</div>
<div id="crayon-5b8f6c187675d765819674-5" class="crayon-line">
 //转换成位于东八区的北京时间则为`1997年7月17日16时20分30秒`
</div>
<div id="crayon-5b8f6c187675d765819674-6" class="crayon-line crayon-striped-line">
 
</div>
<div id="crayon-5b8f6c187675d765819674-7" class="crayon-line">
 1997-07-16T19:20:30+01:00
</div>
<div id="crayon-5b8f6c187675d765819674-8" class="crayon-line crayon-striped-line">
 // 表示东一区的1997年7月16日19时20秒30分,转换成UTC标准时间的话是1997-07-16T18:20:30Z
</div>
</div></td>
</tr>
</tbody>
</table>
  1. 日子和岁月底间的T不得以被总结,一省略就出错。
  2. 虽说在chrome浏览器上时区也得以用+0100那种兰德揽胜FC2822的花样来代表,不过IE上不支持那种混搭写法,所以用ISO860壹规范方式表示的时候时区要用+HH:MM

可是从格式上来讲,两者的界别首要在于分隔符的例外。可是供给注意的是,ISO
860一正经的包容性比讴歌ZDXFC2822差得多(举个例子IE捌和iOS均不协理前者。我知道IE8很多人会无视,不过iOS也有这个坑的话,各位或多或少会谨慎点了吧?),所以一般意况下提议用RFC 2822格式的。
可是需求注意的是,在未钦点期区的前提下,对于只准确到day的日子字符串,RFC 2822回来结果是以当前时区的零点为准,而ISO8601回去结果则会以UTC时间的零点为正规开始展览辨析。
例如:

JavaScript

//中华VFC282②: new Date(‘一玖95/02/1三’) //Thu Feb 13 1玖玖伍 00:00:00 创新霉素T+0800
(中中原人民共和国规范时间) //ISO860一: new Date(‘1991-0贰-1三’) //Thu Feb 一三 一9玖伍0八:00:00 GMT+0800 (中中原人民共和国家规范准时间)

1
2
3
4
//RFC2822:
new Date(‘1992/02/13’) //Thu Feb 13 1992 00:00:00 GMT+0800 (中国标准时间)
//ISO8601:
new Date(‘1992-02-13’) //Thu Feb 13 1992 08:00:00 GMT+0800 (中国标准时间)

 

可是上边这几个只是ES5的专门的职业而已,在ES陆里这三种格局都会形成当前时区的零点为基准1
*不管你们崩溃没,反正我是已经想死了*
有关跨浏览器的dataString解析意况,还是可以参照那个页面:
JavaScript and Dates, What a
Mess!

因而对于时间字符串对象,个人见解是依旧用RFC2822花样,要么自个儿写个解析函数然后随便你传啥格式进来。


相关文章

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图