473,569 Members | 2,716 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why are functions atomic?

Why are functions atomic? (I.e. they are not copied.)

For example, I would like to make a copy of a function so I can change
the default values:
>>from copy import copy
f = lambda x: x
f.func_defaul ts = (1,)
g = copy(f)
g.func_defaul ts = (2,)
f(),g()
(2, 2)

I would like the following behaviour:
>>f(),g()
(1,2)

I know I could use a 'functor' defining __call__ and using member
variables, but this is more complicated and quite a bit slower. (I
also know that I can use new.function to create a new copy, but I
would like to know the rational behind the decision to make functions
atomic before I shoot myself in the foot;-)

Thanks,
Michael.

May 1 '07 #1
31 1760
Michael wrote:
Why are functions atomic? (I.e. they are not copied.)

For example, I would like to make a copy of a function so I can change
the default values:
>>>from copy import copy
f = lambda x: x
f.func_defau lts = (1,)
g = copy(f)
g.func_defau lts = (2,)
f(),g()
(2, 2)

I would like the following behaviour:
>>>f(),g()
(1,2)

I know I could use a 'functor' defining __call__ and using member
variables, but this is more complicated and quite a bit slower. (I
also know that I can use new.function to create a new copy, but I
would like to know the rational behind the decision to make functions
atomic before I shoot myself in the foot;-)

Thanks,
Michael.
This dont make functions copiable but may resolve your default arguments
problem. Under Python 2.5, see functools.parti al().

http://docs.python.org/lib/partial-objects.html

May 1 '07 #2
On May 1, 5:06 am, Michael <michael.for... @gmail.comwrote :
Why are functions atomic? (I.e. they are not copied.)

For example, I would like to make a copy of a function so I can change
the default values:
>from copy import copy
f = lambda x: x
f.func_default s = (1,)
g = copy(f)
g.func_default s = (2,)
f(),g()

(2, 2)

I would like the following behaviour:
>f(),g()

(1,2)

I know I could use a 'functor' defining __call__ and using member
variables, but this is more complicated and quite a bit slower. (I
also know that I can use new.function to create a new copy, but I
would like to know the rational behind the decision to make functions
atomic before I shoot myself in the foot;-)

Thanks,
Michael.
Does deepcopy work?

May 1 '07 #4
7stud <bb**********@y ahoo.comwrote:
Does deepcopy work?
It doesn't copy a function.

The easiest way to make a modified copy of a function is to use the 'new'
module.
>>def f(x=2): print "x=", x
>>g = new.function(f. func_code, f.func_globals, 'g', (3,),
f.func_closure)
>>g()
x= 3
>>f()
x= 2
May 1 '07 #5
On 1 May 2007 15:17:48 GMT, Duncan Booth <du**********@i nvalid.invalidw rote:
7stud <bb**********@y ahoo.comwrote:
Does deepcopy work?

It doesn't copy a function.

The easiest way to make a modified copy of a function is to use the 'new'
module.
>def f(x=2): print "x=", x
>g = new.function(f. func_code, f.func_globals, 'g', (3,),
f.func_closure)
>g()
x= 3
>f()
x= 2
--
http://mail.python.org/mailman/listinfo/python-list
The copy module considers functions to be immutable and just returns
the object. This seems pretty clearly wrong to me - functions are
clearly not immutable and it's easy to copy a function using new, as
shown above.
>From copy.py:
def _copy_immutable (x):
return x
for t in (type(None), int, long, float, bool, str, tuple,
frozenset, type, xrange, types.ClassType ,
types.BuiltinFu nctionType,
types.FunctionT ype):
d[t] = _copy_immutable
May 1 '07 #6
Michael wrote:
Why are functions atomic? (I.e. they are not copied.)
Because Python has objects for when you need to associate
state with a function.

John Nagle
May 1 '07 #7
I know I could use a 'functor' defining __call__ and using member
variables, but this is more complicated and quite a bit slower. (I
also know that I can use new.function to create a new copy, but I
would like to know the rational behind the decision to make functions
atomic before I shoot myself in the foot;-)
Function objects were not copyable because they were immutable. For
an immutable object, a copy cannot reasonably be distinguished from
the original object. See copy.py, around line 105.

Regards,
Martin
May 1 '07 #8
On May 1, 9:34 am, John Nagle <n...@animats.c omwrote:
Michael wrote:
Why are functions atomic? (I.e. they are not copied.)

Because Python has objects for when you need to associate
state with a function.

John Nagle
Then why are functions mutable?

I can understand to some extent why functions are not picklable,
because the bytecode may not be the same across python implementations
(is that true?), but I do not understand why copying functions is a
problem. The patch that allows copy to pass-through functions just
emulates pickle, but I can find no discussion or justification for not
allowing functions to be copied:

http://thread.gmane.org/gmane.comp.python.devel/76636

Michael.

May 1 '07 #9
>From TFM

"Function objects also support getting and setting arbitrary
attributes, which can be used, for example, to attach metadata to
functions. Regular attribute dot-notation is used to get and set such
attributes. Note that the current implementation only supports
function attributes on user-defined functions. Function attributes on
built-in functions may be supported in the future."

http://docs.python.org/ref/types.html

Again, rather inconsitent with the copy sematics.
On May 1, 9:34 am, John Nagle <n...@animats.c omwrote:
Because Python has objects for when you need to associate
state with a function.
John Nagle
May 1 '07 #10

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

Similar topics

99
5851
by: David MacQuigg | last post by:
I'm not getting any feedback on the most important benefit in my proposed "Ideas for Python 3" thread - the unification of methods and functions. Perhaps it was buried among too many other less important changes, so in this thread I would like to focus on that issue alone. I have edited the Proposed Syntax example below to take out the...
8
3568
by: Glenn Kasten | last post by:
I am wondering which operations in Python are guaranteed to be atomic in the presence of multi-threading. In particular, are assignment and reading of a dictionary entry atomic? For example, initially: dictionary = {} dictionary = old_value Then thread 1 does: v = dictionary And thread 2 does:
42
3325
by: Shayan | last post by:
Is there a boolean flag that can be set atomically without needing to wrap it in a mutex? This flag will be checked constantly by multiple threads so I don't really want to deal with the overhead of mutexes or semaphores. Thanks. Shayan
5
23672
by: Pegboy | last post by:
What does it mean to make a function atomic? Something I read on it wasn't very clear, but made me think that I needed to disable interrupts for that function. Whether that's the case or not, how would I make the following function atomic? int Test; int GetTestValue( void ) {
28
7358
by: robert | last post by:
In very rare cases a program crashes (hard to reproduce) : * several threads work on an object tree with dict's etc. in it. Items are added, deleted, iteration over .keys() ... ). The threads are "good" in such terms, that this core data structure is changed only by atomic operations, so that the data structure is always consistent regarding...
6
6240
by: blackstreetcat | last post by:
consider this code : int i; //gobal var Thread1: i=some value; Thread2: if (i==2) dosomething(); else dosomethingelse();
11
2060
by: japhy | last post by:
Is there a way to read a line (a series of characters ending in a newline) from a file (either by descriptor or stream) atomically, without buffering additional contents of the file?
0
1917
by: yogeeswar | last post by:
Hi All I wrote SP with number of delete commands in an atomic block.And there is a possibility of deleting records from parent table before child table so I wrote a handler to handle the exception.in that handler I am using the ROLLBACK to rollback all the records which were deleted if any exception occurs. THE PROBLEM IS: It is working...
2
2929
by: Freedom fighter | last post by:
Hello, Is a singleton class the same as an atomic class? I know that a singleton class can only be instantiated once, but does that concept apply to an atomic class? Thank you.
0
7694
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...
0
7609
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...
0
7921
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. ...
0
8118
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7666
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...
0
7964
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
3651
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...
1
2107
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
936
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...

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.