473,624 Members | 2,135 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Thank you in advance,
Dmitrey

Mar 15 '07 #1
3 2915
In <11************ **********@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

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

Mar 15 '07 #3
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()



Mar 15 '07 #4

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

Similar topics

9
2665
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 computational schemes. The use of Python as my first-choice language has made me extremely productive. Now, I have always believed that Python is a poor performer in terms of numerical speed. My experience, however, is that the efficient use of...
2
2531
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, 'plot(x,y)') This is working fine. However, i dont know how to print or save this plot as a .tif image something like print(gcf, '-dtiff', 'test')
0
1655
by: Srinath Avadhanula | last post by:
Hello, I wanted to connect to an already existing MATLAB application from python. From the MATHWORKS documentation at: http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_external/ch07cl24.html I see that to connect to an already existing MATLAB application, I
4
2751
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 prefer to do the bulk of the project in python because I'm familiar with pygame (for the visuals) but I already have a lot of AI code written in Matlab. I'd like to be able to call Matlab code from within python. I'm not sure this is possible. My...
12
2258
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
2120
by: Allen | last post by:
Does anyone agree with me? If you have used Matlab, welcome to discuss it.
10
5256
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
12401
by: openopt | last post by:
Thank you in advance for your response. Dmitrey
4
2329
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 octave is about 30 time slower than matlab. Here is the result in matlab: Elapsed time is 0.015389 seconds.
0
8234
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8172
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8677
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8335
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7158
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6110
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5563
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4079
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
1482
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.