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

M

Marc 'BlackJack' Rintsch

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_attribute = 42

def __init__(self):
self.i_am_an_instance_attribute = '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
 
S

sjdevnull

Marc said:
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_attribute = 42

def __init__(self):
self.i_am_an_instance_attribute = '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
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).
 
S

sturlamolden

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()
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top