Help understanding code

Discussion in 'Python' started by Dhruva Hein, Apr 10, 2005.

  1. Dhruva Hein

    Dhruva Hein Guest

    Hi. I am trying to understand a section of code written for Plone and I
    am having problems understanding the Python syntax in a few of the
    following lines.

    I'd be grateful if someone could help me understand what's happening in
    the lines I've marked. I can't see how the dictionary is built or how
    the lists ['bytype'][cytype] make sense in python.

    Thanks in advance.

    class Stats:

    def getContentTypes(self):
    """ Returns the number of documents by type """
    pc = getToolByName(self, "portal_catalog")
    # call the catalog and loop through the records
    results = pc()
    <=== what is the difference between pc and pc()?
    numbers = {"total":len(results),"bytype":{},"bystate":{}} <===
    This is hard to understand
    for result in results:
    # set the number for the type
    ctype = str(result.Type)
    num = numbers["bytype"].get(ctype, 0) <==== where does .get
    come from? and what is the string 'bytype' doing?
    num += 1
    numbers["bytype"][ctype] = num <====== is this some kind
    of an array?

    # set the number for the state
    state = str(result.review_state)
    num = numbers["bystate"].get(state, 0)
    num += 1
    numbers["bystate"][state] = num
    return numbers
    Dhruva Hein, Apr 10, 2005
    #1
    1. Advertising

  2. Dhruva Hein

    Guest

    Reading the language tututorial would help you a lot :(

    === what is the difference between pc and pc()?

    pc = getToolByName(....) - returnes a refference to a method

    pc is a refference to a method, pc() is a method invocation.

    ============

    numbers = {"total":len(results),"bytype":{},"bystate":{}}
    <=== This is hard to understand
    num = numbers["bytype"].get(ctype, 0) <==== where does .get
    come from? and what is the string 'bytype' doing?
    numbers["bytype"][ctype] = num <====== is this some kind
    of an array?

    Read the tutorial especially portion about dictionaries!
    , Apr 10, 2005
    #2
    1. Advertising

  3. a écrit :
    > Reading the language tututorial would help you a lot :(
    >
    > === what is the difference between pc and pc()?
    >
    > pc = getToolByName(....) - returnes a refference to a method


    Nope.

    > pc is a refference to a method,
    >

    Nope.

    pc is not 'a reference to a method', it's a callable object (in this
    case a ZCatalog instance...)....

    > pc() is a method invocation.


    In this case, it happens to be a call to a method of ZCatalog, but it
    could have been a call to a named or anonymous function as well...

    <op>
    In Python, functions and methods are objects too, so you can use them
    like any other object :

    def fun():
    print "hello world"

    machin = fun
    machin()
    >> hello world


    But objects can be used like functions too, if they define a __call__
    method:

    class fakeFun(object):
    def __init__(self, name):
    self.name = name
    def __call__(self):
    return "hello, I'm %s" % self.name

    f= fakeFun('foo')
    f()
    >> hello, I'm foo

    </op>

    HTH
    Bruno
    Bruno Desthuilliers, Apr 11, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JM
    Replies:
    3
    Views:
    2,154
    Andy Dingley
    Oct 1, 2005
  2. Fredrik Lundh

    Re: Help understanding code

    Fredrik Lundh, Apr 10, 2005, in forum: Python
    Replies:
    1
    Views:
    331
    Duncan Booth
    Apr 11, 2005
  3. Replies:
    2
    Views:
    416
    Phlip
    Mar 15, 2007
  4. Need help in understanding c code

    , Jun 1, 2007, in forum: C Programming
    Replies:
    2
    Views:
    314
    Barry Schwarz
    Jun 2, 2007
  5. Daniel
    Replies:
    8
    Views:
    434
    Daniel
    Sep 13, 2007
Loading...

Share This Page