what are Python equivalent to MATLAB persistent or C++ static?

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

    #1

    what are Python equivalent to MATLAB persistent or C++ static?

    Thank you in advance,
    Dmitrey

  • Marc 'BlackJack' Rintsch

    #2
    Re: what are Python equivalent to MATLAB persistent or C++ static?

    In <1173942572.130 747.228410@l77g 2000hsb.googleg roups.com>, dmitrey wrote:
    Thank you in advance,
    For what? Hint: Don't "hide" the question in the subject line.

    I don't know MATLAB's `persistent` but I know that ``static`` in C++ can
    be used in different places with different meanings.

    It seems you are asking questions how to translate some constructs from
    other languages 1:1 into Python. Without context this may lead you to
    programming some other language in Python, resulting in fighting the
    language because you don't use "pythonic" idioms to solve your problems.

    C++-static function's names on module level should start with an
    underscore:

    def _i_am_not_meant _to_be_public() :
    pass

    It's a naming convention for things that are considered internal.

    C++-static class members are class attributes in Python:

    class Spam(object):
    i_am_a_class_at tribute = 42

    def __init__(self):
    self.i_am_an_in stance_attribut e = 'Viking'

    And C++static local variables don't exist in Python. There are ways to
    emulate them with mutable default arguments, but that's at least
    debatable. Use a class instead.

    Ciao,
    Marc 'BlackJack' Rintsch

    Comment

    • sjdevnull@yahoo.com

      #3
      Re: what are Python equivalent to MATLAB persistent or C++ static?

      Marc 'BlackJack' Rintsch wrote:
      In <1173942572.130 747.228410@l77g 2000hsb.googleg roups.com>, dmitrey wrote:
      >
      Thank you in advance,
      >
      For what? Hint: Don't "hide" the question in the subject line.
      >
      I don't know MATLAB's `persistent` but I know that ``static`` in C++ can
      be used in different places with different meanings.
      >
      It seems you are asking questions how to translate some constructs from
      other languages 1:1 into Python. Without context this may lead you to
      programming some other language in Python, resulting in fighting the
      language because you don't use "pythonic" idioms to solve your problems.
      >
      C++-static function's names on module level should start with an
      underscore:
      >
      def _i_am_not_meant _to_be_public() :
      pass
      >
      It's a naming convention for things that are considered internal.
      >
      C++-static class members are class attributes in Python:
      >
      class Spam(object):
      i_am_a_class_at tribute = 42
      >
      def __init__(self):
      self.i_am_an_in stance_attribut e = 'Viking'
      >
      And C++static local variables don't exist in Python. There are ways to
      emulate them with mutable default arguments, but that's at least
      debatable. Use a class instead.
      If you must, function attributes emulate static:
      def myfunc():
      myfunc.foo += 1
      return myfunc.foo
      myfunc.foo=0 #initialize the value near the function definition
      >>print myfunc()
      1
      >>print myfunc()
      2
      etc

      But it's usually a bad idea to use this. function attributes are
      better reserved for information about the function itself (similar to
      docstrings).

      Comment

      • sturlamolden

        #4
        Re: what are Python equivalent to MATLAB persistent or C++ static?

        On Mar 15, 8:09 am, "dmitrey" <open...@ukr.ne twrote:
        Thank you in advance,
        Dmitrey
        First, "static" can mean at least three different things in C++:

        static int myvar1;

        void foobar() {
        static int myvar2;
        }

        class foobar {
        static int myvar3;
        }

        I assume you are thinking about the second case above - a static
        variable inside a function. You can achieve this by binding an
        attribute to the function, use a closure, or declare a class
        callable.

        def foobar1():
        if not 'mystatic' in dir(foobar): foobar.mystatic = 0
        foobar.mystatic += 1
        return foobar.mystatic

        def foobar2():
        mystatic = 0
        def closure():
        mystatic += 1
        return mystatic
        return closure

        class foobar3:
        mystatic = 0
        def __call__():
        foobar.mystatic += 1
        return foobar.mystatic

        Usage:

        for i in xrange(0,10): print foobar1()

        myclosure = foobar2()
        for i in xrange(0,10): print myclosure()

        myfoobar3 = foobar3()
        for i in xrange(0,10): print myfoobar3()









        Comment

        Working...