Hello everybody!
I have little problem:
class A:
def __init__(self, n):
self.data = n
def f(self, x = ????)
print x
All I want is to make self.data the default argument for self.f(). (I
want to use 'A' class as following :
myA = A(5)
myA.f()
and get printed '5' as a result.)
Nov 22 '05
44 2774
Mike Meyer wrote, in part:: "Gregory Petrosyan" <gr************ ***@gmail.com> writes: ... 2) Is 'foo.s = n' a correct solution? It seems to be a little more elegant. (I tested it, and it worked well)
It's basically the same solution. You're replacing binding a variable with mutating an object bound to a name in an outer scope. In one case the container is named s and is a list that you're setting an element of. In the other case, the container is named foo and is an object that you're setting an attribute on.
Well, perhaps the same in the sense of name binding, but there's a
subtle difference in replacing the 's = [n]' with 'foo.s = n'. Namely
that in the former case (with the essay's original code) a separate
container is created when foo() is first called and is what is used in
subsequent calls to the function returned. Whereas in the latter case
where the foo object itself is used as the container, there's only a
single container used by all returned objects -- which would cause
problems if you try accumulating two or more different totals
simultaneously.
Here's a very contrived test case which illustrates the point I'm
trying to make:
def foo(n):
foo.s = n
def bar(i):
foo.s += i
return foo.s
return bar
a1 = foo(0)
a2 = foo(0)
print "before a1(0):", a1(0)
print "before a2(0):", a2(0)
a1(1)
a2(1)
print "after a1(0):", a1(0)
print "after a2(0):", a2(0) outputs
before a1(0): 0
before a2(0): 0
after a1(0): 2
after a2(0): 2
Notice that it even though each was only incremented by 1 once, they
interacted, and show the effects of two calls. This doesn't happen in
in Paul Graham's version, where the two 'after' calls would correctly
retrun a value of 1.
-Martin
Thanks Martin, you are right.
Thanks Martin, you are right.
Martin Miller wrote: Well, perhaps the same in the sense of name binding, but there's a subtle difference in replacing the 's = [n]'**with*'foo.s* =*n'.**Namely that in the former case (with the essay's original code) a separate container is created when foo() is first called and is what is used in subsequent calls to the function returned.**Wher eas*in*the*latt er*case where the foo object itself is used as the container, there's only a single container used by all returned objects -- which would cause problems if you try accumulating two or more different totals simultaneously.
[snip example using the outer foo() as a container]
You can easily get a unique container using the function attribute style, to
-- just use the inner function bar(): def foo(n):
.... def bar(i):
.... bar.i += 1
.... re
.... def foo(n):
.... def bar(i):
.... bar.s += i
.... return bar.s
.... bar.s = n
.... return bar
.... a1 = foo(0) a2 = foo(0) a1(0), a2(0)
(0, 0) a1(1), a2(1)
(1, 1)
Peter
Martin Miller wrote: Well, perhaps the same in the sense of name binding, but there's a subtle difference in replacing the 's = [n]'**with*'foo.s* =*n'.**Namely that in the former case (with the essay's original code) a separate container is created when foo() is first called and is what is used in subsequent calls to the function returned.**Wher eas*in*the*latt er*case where the foo object itself is used as the container, there's only a single container used by all returned objects -- which would cause problems if you try accumulating two or more different totals simultaneously.
[snip example using the outer foo() as a container]
You can easily get a unique container using the function attribute style, to
-- just use the inner function bar(): def foo(n):
.... def bar(i):
.... bar.i += 1
.... re
.... def foo(n):
.... def bar(i):
.... bar.s += i
.... return bar.s
.... bar.s = n
.... return bar
.... a1 = foo(0) a2 = foo(0) a1(0), a2(0)
(0, 0) a1(1), a2(1)
(1, 1)
Peter This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Edward Diener |
last post by:
In the tutorial on functions there are sections on default arguments and
keyword arguments, yet I don't see the syntactic difference between them.
For default arguments the tutorial shows:
def ask_ok(prompt, retries=4, complaint='Yes or no, please!'):
while for keyword arguments the tutorial shows:
def parrot(voltage, state='a stiff', action='voom', type='Norwegian Blue'):
|
by: earl |
last post by:
class temp
{
public:
temp();
foo(char, char, char*);
private:
char matrix;
};
temp::foo(char p, char o, char m = matrix )
|
by: steve |
last post by:
Hi all
If I use this code
<% If Request.Item("col")="firstcoll" Then Response.Write "Checked"%>
in .aspx page for example like that
<input type="radio" name="col" value="firstcoll" <% If
Request.Item("col")="firstcoll" Then Response.Write "Checked"%>>
|
by: steve |
last post by:
Sorry if I have post this two times.
Hi all
What I'm trying to achieve is:
I have a form with two radio buttons,
if a visitors check radio button 2 and click submit in the next page
radio button 2 to be check by default
If I use this code
|
by: Matt |
last post by:
I try to compare the default constructor in Java and C++.
In C++, a default constructor has one of the two meansings
1) a constructor has ZERO parameter
Student()
{ //etc...
}
2) a constructor that all parameters have default values
| |
by: cody |
last post by:
Why doesn't C# allow default parameters for methods?
An argument against I hear often is that the default parameters would have
to be hardbaken into the assembly, but why? The Jit can take care of this,
if the code is jitted the
"push xyz" instructions of the actual default values can be inserted.
To make things simpler and better readable I'd make all default parameters
named parameters so that you can decide for yourself why one to...
|
by: cody |
last post by:
I got a similar idea a couple of months ago, but now this one will require
no change to the clr, is relatively easy to implement and would be a great
addition to C# 3.0 :)
so here we go..
To make things simpler and better readable I'd make all default parameters
named parameters so that you can decide for yourself which one to pass and
which not, rather than relying on massively overlaoded methods which
hopefully provide the best...
|
by: prefersgolfing |
last post by:
In 6, by default arguments, were passed byref what is the default in .NET?
Can you point me to any MSDN documentation? TIA
|
by: aboutjav.com |
last post by:
Hi,
I need some help. I am getting this error after I complete the asp.net
register control and click on the continue button. It crashed when it
tries to get
it calls this Profile property
((string)(this.GetPropertyValue("Address1")));
|
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...
|
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,...
| |
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...
|
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,...
|
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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...
| |