欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

如何遍歷python中的對象屬性

 更新時(shí)間:2023年11月10日 11:15:31   作者:Rnan-prince  
這篇文章主要介紹了如何遍歷python中的對象屬性問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

遍歷python中的對象屬性

python開發(fā)中有時(shí)候需要遍歷某對象的屬性list

class Person(object):
    """
    職員信息
    """
    name = 0
    age = 1
    id = 2
    group = 3
 
 
attr = [a for a in dir(Person) if not a.startswith('__')]
for a in attr:
    print(a, getattr(Person, a))
 
"""
age 1
group 3
id 2
name 0
"""

迭代器自定義遍歷對象

要說起迭代器自定義遍歷對象,首頁要知道什么是迭代器?

生成器 概念在Java,Python等語言中都是具備的,ES6也添加到了JavaScript中。Iterator可以使我們不需要初始化集合,以及索引的變量,而是使用迭代器對象的 next 方法,返回集合的下一項(xiàng)的值,偏向程序化。

迭代器 是帶有特殊接口的對象。含有一個(gè)next()方法,調(diào)用返回一個(gè)包含兩個(gè)屬性的對象,分別是value和done,value表示當(dāng)前位置的值done表示是否迭代完,當(dāng)為true的時(shí)候,調(diào)用next就無效了。

ES5中遍歷集合通常都是 for循環(huán),數(shù)組還有 forEach 方法,對象就是 for-in,ES6 中又添加了 MapSet,而迭代器可以統(tǒng)一處理所有集合數(shù)據(jù)的方法。迭代器是一個(gè)接口,只要你這個(gè)數(shù)據(jù)結(jié)構(gòu)暴露了一個(gè)iterator的接口,那就可以完成迭代。

ES6創(chuàng)造了一種新的遍歷命令for...of循環(huán),Iterator接口主要供for...of消費(fèi)。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>迭代器</title>
</head>
<body>
    <script>
        //聲明一個(gè)數(shù)組
        const xiyou = ['唐僧','孫悟空','豬八戒','沙僧'];
 
        //使用 for...of 遍歷數(shù)組
        // for(let v of xiyou){
        //     console.log(v);
        // }
 
        let iterator = xiyou[Symbol.iterator]();
 
        //調(diào)用對象的next方法
        console.log(iterator.next());
        console.log(iterator.next());
        console.log(iterator.next());
        console.log(iterator.next());
        console.log(iterator.next());
    </script>
</body>
</html>

如上所示,可以使用 for...of 遍歷數(shù)組,直接得到值的遍歷。也可以使用迭代器的方式進(jìn)行遍歷。

迭代器自定義遍歷對象遍歷對象中的某一個(gè)屬性?

舉例:使用for..of遍歷banji中stus數(shù)組?

<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>自定義遍歷數(shù)據(jù)</title>
</head>
 
<body>
    <script>
        //聲明一個(gè)對象
        const banji = {
            name: "終極一班",
            stus: [
                'xiaoming',
                'xiaoning',
                'xiaotian',
                'knight'
            ],
            [Symbol.iterator]() {
                //索引變量
                let index = 0;
                //保存外面的this對象
                let _this = this;
                return {
                    next: function () {
                        if (index < _this.stus.length) {
                            const result = { value: _this.stus[index], done: false };
                            //下標(biāo)自增
                            index++;
                            //返回結(jié)果
                            return result;
                        }else{
                            return {value: undefined, done: true};
                        }
                    }
                };
            }
        }
 
        //遍歷這個(gè)對象 
        for (let v of banji) {
            console.log(v);
        }
    </script>
</body>
 
</html>

如上所示,我們使用自定義的一個(gè)[Symbol.iterator](){ }函數(shù),在內(nèi)部完成一個(gè)迭代器的操作,然后在外部通過for..of遍歷,即可實(shí)現(xiàn)效果;

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論