why function got dictionary

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • AlFire

    why function got dictionary

    Hi,

    I am seeking an explanation for following:

    Python 2.5.2 (r252:60911, Apr 8 2008, 21:49:41)
    [GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
    Type "help", "copyright" , "credits" or "license" for more information.
    >>def g(): return
    ....
    >>g.__dict__
    {}

    Q: why function got dictionary? What it is used for?


    Thx, Andy
  • Diez B. Roggisch

    #2
    Re: why function got dictionary

    AlFire wrote:
    Hi,
    >
    I am seeking an explanation for following:
    >
    Python 2.5.2 (r252:60911, Apr 8 2008, 21:49:41)
    [GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
    Type "help", "copyright" , "credits" or "license" for more information.
    >>def g(): return
    ...
    >>g.__dict__
    {}
    >
    Q: why function got dictionary? What it is used for?
    because it is an object, and you can do e.g.

    g.exposed = True

    or similar stuff.

    Diez

    Comment

    • bruno.desthuilliers@gmail.com

      #3
      Re: why function got dictionary

      On 17 avr, 16:06, AlFire <spamgrinder.tr yla...@gmail.co mwrote:
      Hi,
      >
      I am seeking an explanation for following:
      >
      Python 2.5.2 (r252:60911, Apr 8 2008, 21:49:41)
      [GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
      Type "help", "copyright" , "credits" or "license" for more information.
      >>def g(): return
      ...
      >>g.__dict__
      {}
      >
      Q: why function got dictionary? What it is used for?
      A: everything (or almost) in Python is an object. Including functions,
      classes, modules etc.


      Comment

      • AlFire

        #4
        Re: why function got dictionary

        Diez B. Roggisch wrote:
        >>
        >Q: why function got dictionary? What it is used for?
        >
        because it is an object, and you can do e.g.
        >
        you mean an object in the following sense?
        >> isinstance(g,ob ject)
        True


        where could I read more about that?

        Andy

        Comment

        • Diez B. Roggisch

          #5
          Re: why function got dictionary

          AlFire wrote:
          Diez B. Roggisch wrote:
          >>>
          >>Q: why function got dictionary? What it is used for?
          >>
          >because it is an object, and you can do e.g.
          >>
          >
          you mean an object in the following sense?
          >
          >> isinstance(g,ob ject)
          True
          Yes.
          >
          where could I read more about that?
          I don't know. But essentially _everything_ in python is an object. Some of
          them lack a __dict__ - e.g. int and float and such - for optimization
          reasons. But apart from that, you can treat everything as an object.

          Diez

          Comment

          • Tim Roberts

            #6
            Re: why function got dictionary

            AlFire <spamgrinder.tr ylater@gmail.co mwrote:
            >Diez B. Roggisch wrote:
            >>>
            >>Q: why function got dictionary? What it is used for?
            >>
            >because it is an object, and you can do e.g.
            >>
            >
            >you mean an object in the following sense?
            >
            > isinstance(g,ob ject)
            >True
            >
            >where could I read more about that?
            This is a very useful feature. As just one example, the CherryPy web
            server framework lets you define a class to handle one directory in a web
            site, where each page is mapped to member functions. However, you also
            need to include support functions that should not be exposed as web pages.
            To do that, you just add an "exposed" attribute to the function:

            class MyWebPage:
            ...
            def index( self, ... ):
            pass
            index.exposed = 1

            def notExposed( self, ... ):
            pass

            def request( self, ... ):
            pass
            request.exposed = 1
            --
            Tim Roberts, timr@probo.com
            Providenza & Boekelheide, Inc.

            Comment

            • Christian Heimes

              #7
              Re: why function got dictionary

              bruno.desthuill iers@gmail.com schrieb:
              A: everything (or almost) in Python is an object. Including functions,
              classes, modules etc.
              Everything you can access from or through Python code must be an object.
              Every object has at least a type and a reference count.

              Christian

              Comment

              • sturlamolden

                #8
                Re: why function got dictionary

                On Apr 17, 4:06 pm, AlFire <spamgrinder.tr yla...@gmail.co mwrote:
                Q: why function got dictionary? What it is used for?
                As previously mentioned, a function has a __dict__ like (most) other
                objects.

                You can e.g. use it to create static variables:

                int foobar()
                {
                static int i = 0;
                return i++;
                }

                is roughly equivalent to:

                def foobar():
                foobar.i += 1
                return foobar.i
                foobar.i = 0







                Comment

                • bruno.desthuilliers@gmail.com

                  #9
                  Re: why function got dictionary

                  On 19 avr, 19:39, sturlamolden <sturlamol...@y ahoo.nowrote:
                  On Apr 17, 4:06 pm, AlFire <spamgrinder.tr yla...@gmail.co mwrote:
                  >
                  Q: why function got dictionary? What it is used for?
                  >
                  As previously mentioned, a function has a __dict__ like (most) other
                  objects.
                  >
                  You can e.g. use it to create static variables:
                  >
                  int foobar()
                  {
                  static int i = 0;
                  return i++;
                  >
                  }
                  >
                  is roughly equivalent to:
                  >
                  def foobar():
                  foobar.i += 1
                  return foobar.i
                  foobar.i = 0
                  barfoo = foobar
                  foobar = lambda x : x

                  And boom.

                  'static' variables are better implemented using either closures,
                  mutable default arguments or custom callable types.

                  Comment

                  • sturlamolden

                    #10
                    Re: why function got dictionary

                    On Apr 19, 8:33 pm, "bruno.desthuil li...@gmail.com "
                    <bruno.desthuil li...@gmail.com wrote:
                    barfoo = foobar
                    foobar = lambda x : x
                    >
                    And boom.
                    That's why I used the qualifier 'roughly equivalent' and not simply
                    'equivalent'.


                    Comment

                    • Gabriel Genellina

                      #11
                      Re: why function got dictionary

                      En Thu, 17 Apr 2008 11:06:08 -0300, AlFire <spamgrinder.tr ylater@gmail.co mescribió:
                      Q: why function got dictionary? What it is used for?
                      If you want more details, see PEP 232 that introduced function writable attributes in Python 2.1: http://www.python.org/dev/peps/pep-0232/

                      --
                      Gabriel Genellina

                      Comment

                      Working...