Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用Python实现表_Python - Fatal编程技术网

用Python实现表

用Python实现表,python,Python,我在Udacity在线课程上使用Google App Engine,这段代码让我很困惑。我还没见过Python用这种方式。老师没有在全班复习NameTuple,所以这对我来说是出乎意料的。有人能解释一下这段代码是如何工作的吗?有没有更简单的解释,为什么Steve Huffman更喜欢这种Python编码风格 from collections import namedtuple # make a basic Link class Link = namedtuple('Link', ['id',

我在Udacity在线课程上使用Google App Engine,这段代码让我很困惑。我还没见过Python用这种方式。老师没有在全班复习NameTuple,所以这对我来说是出乎意料的。有人能解释一下这段代码是如何工作的吗?有没有更简单的解释,为什么Steve Huffman更喜欢这种Python编码风格

from collections import namedtuple

# make a basic Link class
Link = namedtuple('Link', ['id', 'submitter_id', 'submitted_time', 'votes',
                           'title', 'url'])

# list of Links to work with
links = [
    Link(0, 60398, 1334014208.0, 109,
         "C overtakes Java as the No. 1 programming language in the TIOBE index.",
         "http://pixelstech.net/article/index.php?id=1333969280"),
    Link(1, 60254, 1333962645.0, 891,
         "This explains why technical books are all ridiculously thick and overpriced",
         "http://prog21.dadgum.com/65.html"),
    Link(23, 62945, 1333894106.0, 351,
         "Learn Haskell Fast and Hard",
         "http://yannesposito.com/Scratch/en/blog/Haskell-the-Hard-Way/"),
    Link(2, 6084, 1333996166.0, 81,
         "Announcing Yesod 1.0- a robust, developer friendly, high performance web framework for Haskell",
         "http://www.yesodweb.com/blog/2012/04/announcing-yesod-1-0"),
    Link(3, 30305, 1333968061.0, 270,
         "TIL about the Lisp Curse",
         "http://www.winestockwebdesign.com/Essays/Lisp_Curse.html"),
    Link(4, 59008, 1334016506.0, 19,
         "The Downfall of Imperative Programming. Functional Programming and the Multicore Revolution",
         "http://fpcomplete.com/the-downfall-of-imperative-programming/"),
    Link(5, 8712, 1333993676.0, 26,
         "Open Source - Twitter Stock Market Game - ",
         "http://www.twitstreet.com/"),
    Link(6, 48626, 1333975127.0, 63,
         "First look: Qt 5 makes JavaScript a first-class citizen for app development",
         "http://arstechnica.com/business/news/2012/04/an-in-depth-look-at-qt-5-making-javascript-a-first-class-citizen-for-native-cross-platform-developme.ars"),
    Link(7, 30172, 1334017294.0, 5,
         "Benchmark of Dictionary Structures", "http://lh3lh3.users.sourceforge.net/udb.shtml"),
    Link(8, 678, 1334014446.0, 7,
         "If It's Not on Prod, It Doesn't Count: The Value of Frequent Releases",
         "http://bits.shutterstock.com/?p=165"),
    Link(9, 29168, 1334006443.0, 18,
         "Language proposal: dave",
         "http://davelang.github.com/"),
    Link(17, 48626, 1334020271.0, 1,
         "LispNYC and EmacsNYC meetup Tuesday Night: Large Scale Development with Elisp ",
         "http://www.meetup.com/LispNYC/events/47373722/"),
    Link(101, 62443, 1334018620.0, 4,
         "research!rsc: Zip Files All The Way Down",
         "http://research.swtch.com/zip"),
    Link(12, 10262, 1334018169.0, 5,
         "The Tyranny of the Diff",
         "http://michaelfeathers.typepad.com/michael_feathers_blog/2012/04/the-tyranny-of-the-diff.html"),
    Link(13, 20831, 1333996529.0, 14,
         "Understanding NIO.2 File Channels in Java 7",
         "http://java.dzone.com/articles/understanding-nio2-file"),
    Link(15, 62443, 1333900877.0, 1244,
         "Why vector icons don't work",
         "http://www.pushing-pixels.org/2011/11/04/about-those-vector-icons.html"),
    Link(14, 30650, 1334013659.0, 3,
         "Python - Getting Data Into Graphite - Code Examples",
         "http://coreygoldberg.blogspot.com/2012/04/python-getting-data-into-graphite-code.html"),
    Link(16, 15330, 1333985877.0, 9,
         "Mozilla: The Web as the Platform and The Kilimanjaro Event",
         "https://groups.google.com/forum/?fromgroups#!topic/mozilla.dev.planning/Y9v46wFeejA"),
    Link(18, 62443, 1333939389.0, 104,
         "github is making me feel stupid(er)",
         "http://www.serpentine.com/blog/2012/04/08/github-is-making-me-feel-stupider/"),
    Link(19, 6937, 1333949857.0, 39,
         "BitC Retrospective: The Issues with Type Classes",
         "http://www.bitc-lang.org/pipermail/bitc-dev/2012-April/003315.html"),
    Link(20, 51067, 1333974585.0, 14,
         "Object Oriented C: Class-like Structures",
         "http://cecilsunkure.blogspot.com/2012/04/object-oriented-c-class-like-structures.html"),
    Link(10, 23944, 1333943632.0, 188,
         "The LOVE game framework version 0.8.0 has been released - with GLSL shader support!",
         "https://love2d.org/forums/viewtopic.php?f=3&t=8750"),
    Link(22, 39191, 1334005674.0, 11,
         "An open letter to language designers: Please kill your sacred cows. (megarant)",
         "http://joshondesign.com/2012/03/09/open-letter-language-designers"),
    Link(21, 3777, 1333996565.0, 2,
         "Developers guide to Garage48 hackatron",
         "http://martingryner.com/developers-guide-to-garage48-hackatron/"),
    Link(24, 48626, 1333934004.0, 17,
         "An R programmer looks at Julia",
         "http://www.r-bloggers.com/an-r-programmer-looks-at-julia/")]


# links is a list of Link objects. Links have a handful of properties. For
# example, a Link's number of votes can be accessed by link.votes if "link" is a
# Link.

# make the function query() return the number of votes for the link whose ID is
# 15

def query():
    for l in links:
        if l.id == 15:
            return l.votes

print query()

namedtuple
用于创建基本的
struct
类对象:

Link = namedtuple('Link', ['id', 'submitter_id', 'submitted_time', 'votes',
                       'title', 'url'])
…并像这样使用它:

some_link = Link(6, 48626, 1333975127.0, 63,
                 "Some title", "http://www.theurl.com")
print some_link.url
print some_link.id
.
.
.
使用此功能,您可以以非常简单的方式创建一个对象,例如具有属性
id
submitter\u id
等的对象

您还可以通过类定义来实现:

class Link(object):
    __init__(self, id, submitter_id, submitted_time, votes, title, url):
        self.id = id
        self.submitter_id = submitter_id
        self.submitted_time = submitted_time
        self.votes = votes
        self.title = title
        self.url = 

但是,正如您所看到的,它要“复杂得多”;-)

namedtuple
用于创建基本的
struct
类对象:

Link = namedtuple('Link', ['id', 'submitter_id', 'submitted_time', 'votes',
                       'title', 'url'])
…并像这样使用它:

some_link = Link(6, 48626, 1333975127.0, 63,
                 "Some title", "http://www.theurl.com")
print some_link.url
print some_link.id
.
.
.
使用此功能,您可以以非常简单的方式创建一个对象,例如具有属性
id
submitter\u id
等的对象

您还可以通过类定义来实现:

class Link(object):
    __init__(self, id, submitter_id, submitted_time, votes, title, url):
        self.id = id
        self.submitter_id = submitter_id
        self.submitted_time = submitted_time
        self.votes = votes
        self.title = title
        self.url = 

但是,正如您所看到的,它要“复杂得多”;-)

这里是指向此函数的Python文档的链接:

第一段给出了他可能想要使用这种风格的原因:

命名元组为元组中的每个位置分配意义,并允许更可读、自文档化的代码。它们可以在任何使用常规元组的地方使用,并且它们增加了通过名称而不是位置索引访问字段的能力


以下是指向此函数的Python文档的链接:

第一段给出了他可能想要使用这种风格的原因:

命名元组为元组中的每个位置分配意义,并允许更可读、自文档化的代码。它们可以在任何使用常规元组的地方使用,并且它们增加了通过名称而不是位置索引访问字段的能力


你的问题是什么?它与问题标题有什么关系?请仅发布生成可运行脚本的相关代码,以再现您的问题。包含所有
链接
项会产生代码噪音,使帖子难以阅读您的问题是什么?它与问题标题有什么关系?请仅发布生成可运行脚本的相关代码,以再现您的问题。包含所有
链接
项会产生代码噪音,使文章难以阅读