473,396 Members | 2,011 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

Thank you in advance,
Dmitrey

Mar 15 '07 #1
3 2903
In <11**********************@l77g2000hsb.googlegroups .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_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

Mar 15 '07 #2
Marc 'BlackJack' Rintsch wrote:
In <11**********************@l77g2000hsb.googlegroups .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_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
>>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).

Mar 15 '07 #3
On Mar 15, 8:09 am, "dmitrey" <open...@ukr.netwrote:
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()



Mar 15 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Carl | last post by:
I have been using Python for quite some time now and I love it. I use it mainly for explorative computing and numerical prototyping, ie testing and trying out different kinds of algorithms and...
2
by: Leeny | last post by:
Hi Group, Hope someone can help me with this problem. I am a new user to python and am currently working with pymat module of python. I tried to create plots thru pymat using pymat.eval(h,...
0
by: Srinath Avadhanula | last post by:
Hello, I wanted to connect to an already existing MATLAB application from python. From the MATHWORKS documentation at: ...
4
by: dataangel | last post by:
I'm a student who's considering doing a project for a Machine Learning class on pathing (bots learning to run through a maze). The language primarily used by the class has been Matlab. I would...
12
by: rodchar | last post by:
hey all, vb has a static keyword for variables, what is charp's equivalent,please? static tempVar as String thanks, rodchar
8
by: Allen | last post by:
Does anyone agree with me? If you have used Matlab, welcome to discuss it.
10
by: Chao | last post by:
I've been trying to develop some numerical codes with python, however got disappointed. A very simple test, a = 1.0 for i in range(1000): for j in range(1000): a = a+1
6
by: openopt | last post by:
Thank you in advance for your response. Dmitrey
4
by: wang frank | last post by:
Hi, While comparing the speed of octave and matlab, I decided to do a similar test for python and matlab. The result shows that python is slower than matlab by a factor of 5. It is not bad since...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.