On 28/03/17 17:45, Rafael Knuth wrote:
> Question: When should I use functions?
> When should I use classes?
Thee is no definitive answer but here are some
guidelines:
1) Many instances -> class
2) Many methods sharing the same data -> class
3) An algorithm that has no side effects -> a function
Question: When should I use functions?
When should I use classes?
I wrote my program twice: as a function and as a class (I did so for
educational purposes, to better understand both concepts).
Both programs do exactly the same, and the work neatly. Can you advise
when I should use functions and
On Tue, Dec 15, 2015 at 11:19:33AM +, Matthew Lintern wrote:
> Hello,
>
> I'm new to python and I've been following some youtube videos etc to learn
> python. I'm using the spyder IDE. Im having problems with the following
> piece of code:
>
> def myfun(x):
> y=x**2
> return y
>
> p
On 15/12/15 11:19, Matthew Lintern wrote:
> def myfun(x):
> y=x**2
> return y
>
> print myfun(5)
>
> However, Spyder tells me there's a syntax error with myfun(5).
I suspect that the video is for Python v2 and you are using
Python v3. v3 was a major change in the language and on of
the
Hello,
I'm new to python and I've been following some youtube videos etc to learn
python. I'm using the spyder IDE. Im having problems with the following
piece of code:
def myfun(x):
y=x**2
return y
print myfun(5)
the answer I should get is obviously 25, and this is the case in the vid
Found the solution
On Sun, Mar 29, 2015 at 2:56 PM, Calvin Thai wrote:
> I'm currently making a pygame using IDLE and i'm having a minor issue with
> my functions not changing color. My text is all plain black and its
> bothering me. Is there a solution or can i make my program using another
> t
I'm currently making a pygame using IDLE and i'm having a minor issue with
my functions not changing color. My text is all plain black and its
bothering me. Is there a solution or can i make my program using another
text editor?
(I tried using notepad++ but i don't know how to run the program, it o
On 2015-01-27 16:10, Steven D'Aprano wrote:
On Tue, Jan 27, 2015 at 08:39:17AM -0800, Alex Kleider wrote:
I use the docopt module to collect command line options and then
configparser to read a file.
Some of the values (such as a port number, for example) must then be
adjusted. An example is
a
On Tue, Jan 27, 2015 at 08:39:17AM -0800, Alex Kleider wrote:
> I use the docopt module to collect command line options and then
> configparser to read a file.
> Some of the values (such as a port number, for example) must then be
> adjusted. An example is
> a port number which I want to conver
On 2015-01-27 00:15, Alan Gauld wrote:
On 27/01/15 02:04, Alex Kleider wrote:
Please correct me if I am wrong, but I've assumed that it is proper to
define all functions before embarking on the main body of a program.
No, you can do it either way round. Top-Down or Bottom-Up as they are
known
On 2015-01-26 22:30, Ben Finney wrote:
Alex Kleider writes:
Please correct me if I am wrong, but I've assumed that it is proper to
define all functions before embarking on the main body of a program.
I would say rather that as much code as possible should be in small
well-defined functions,
On 2015-01-27 03:18, Steven D'Aprano wrote:
On Mon, Jan 26, 2015 at 06:04:10PM -0800, Alex Kleider wrote:
Please correct me if I am wrong, but I've assumed that it is proper to
define all functions before embarking on the main body of a program.
I find myself breaking this rule because I want to
On Mon, Jan 26, 2015 at 06:04:10PM -0800, Alex Kleider wrote:
> Please correct me if I am wrong, but I've assumed that it is proper to
> define all functions before embarking on the main body of a program.
> I find myself breaking this rule because I want to set the default
> values of some named
On 27/01/15 02:04, Alex Kleider wrote:
Please correct me if I am wrong, but I've assumed that it is proper to
define all functions before embarking on the main body of a program.
No, you can do it either way round. Top-Down or Bottom-Up as they are known.
Usually its a muxture of both.
But mo
Alex Kleider writes:
> Please correct me if I am wrong, but I've assumed that it is proper to
> define all functions before embarking on the main body of a program.
I would say rather that as much code as possible should be in small
well-defined functions, with the “main body” a tiny and trivial
Please correct me if I am wrong, but I've assumed that it is proper to
define all functions before embarking on the main body of a program.
I find myself breaking this rule because I want to set the default
values of some named function parameters based on a configuration file
which I have to fi
On 13/11/12 03:56, Rufino Beniga wrote:
def IterateLogistic(x,r,n):
for i in xrange(n):
x = r*(1-x)
if i = n:
print x
DogWalker has answered your basic question.
But you don't really need the test at all.
Just print x after the loop finishes:
def IterateL
On Monday 2012 November 12 21:07, you wrote:
> I tried it with i == n as well and it still doesnt work :/
Check the documentation on range and xrange and you will find out why i never
equals n.
>>> n = 5
>>> range(n)
[0, 1, 2, 3, 4]
>>> for i in xrange(n): print i
...
0
1
2
3
4
>>>
--
Yonder no
On Monday 2012 November 12 19:56, Rufino Beniga wrote:
> def IterateLogistic(x,r,n):
> for i in xrange(n):
> x = r*(1-x)
> if i = n:
> print x
>
> I want this function to take in x and r which can be any two real numbers
> and after a certain number of iterations (n)
def IterateLogistic(x,r,n):
for i in xrange(n):
x = r*(1-x)
if i = n:
print x
I want this function to take in x and r which can be any two real numbers
and after a certain number of iterations (n), the function should print the
current state which is x. I tried this
Prasad, Ramit wrote:
Interesting thread and webpages. Insightful, but is this really
used as a technique in daily practice? It feels a bit like a hack
to me. Like the author of one of the websites said: rule #1 don't
mess with this.
I think the problem with rule #1 is that this can occur when y
On 21/10/11 21:40, Albert-Jan Roskam wrote:
Interesting thread and webpages. Insightful, but is this really used as
a technique in daily practice?
Yes, one example is where you use it for a counter to
determine how often a function gets called:
def reserveScarceResource(p1,p2,count = [0]): :
>Interesting thread and webpages. Insightful, but is this really used as a
>technique in daily practice? It feels a bit like a hack to me. Like the author
>of one of the websites said: rule #1 don't mess with this.
I think the problem with rule #1 is that this can occur when you do *not*
under
or us?
~~
>
>From: "Prasad, Ramit"
>To: "tutor@python.org"
>Sent: Friday, October 21, 2011 9:40 PM
>Subject: Re: [Tutor] functions and default argument
>
>>The same thing occurs when you use a mutable object like a list or a
>>dict. The default
>The same thing occurs when you use a mutable object like a list or a
>dict. The default value is assigned once, and once only. But notice that
>you can modify the default value, say by appending to it:
Not sure this will work exactly the same way in other IDEs, but in mine:
>>> a = []
>>> def
Praveen Singh wrote:
In function-
"Default value is *evaluated only once*.This makes different when the
default is a mutable object such as a list, dictionary or instance of most
classes."
I am not getting it properly-evaluated once?? different behaviour???--
please explain this.
Look at an
On 2011/10/21 03:00 PM, Praveen Singh wrote:
In function-
"Default value is *evaluated only once*.This makes different when the
default is a mutable object such as a list, dictionary or instance of
most classes."
I am not getting it properly-evaluated once?? different behaviour???--
please
In function-
"Default value is *evaluated only once*.This makes different when the
default is a mutable object such as a list, dictionary or instance of most
classes."
I am not getting it properly-evaluated once?? different behaviour???--
please explain this.
_
On Fri, 24 Sep 2010 05:32:55 am Alex Hall wrote:
> Hi all,
> A general coding question: is it better to use return(False) (or 0,
> or -1, or whatever) or to raise whateverError("oops")? Are there
> cases for each?
Yes.
There is absolutely no point whatsoever raising an exception, or
returning a
On Fri, 24 Sep 2010 05:47:21 am Wayne Werner wrote:
> OTOH, a lot of people feel that using exceptions as control flow is
> bad practice - they're exceptional so they should only arise in
> exceptional case.
That's not the Python philosophy. Python uses exceptions for flow
control: iteration cat
On Fri, 24 Sep 2010 06:06:25 am Luke Paireepinart wrote:
> You should do both. Raise an exception in the exceptional case.
>
> My general pattern is to return None if no results exist, return the
> results if they do exist, and raise an exception if I couldn't
> perform the function.
I hate that!
Thanks for the responses. Up to now, despite using some Java and a lot
of Python, I have not even tried raising exceptions. I can see
situations where they would be useful, but was not sure if I should
use them as much as possible or just keep relying on the return codes
that I am used to. Sounds l
You should do both. Raise an exception in the exceptional case.
My general pattern is to return None if no results exist, return the results if
they do exist, and raise an exception if I couldn't perform the function.
Eg. If I have a function that creates a list of users with a first name of bob
On Thu, Sep 23, 2010 at 2:32 PM, Alex Hall wrote:
> Hi all,
> A general coding question: is it better to use return(False) (or 0, or
> -1, or whatever) or to raise whateverError("oops")? Are there cases
> for each?
It depends on your prevailing philosophy - if you like the EAFP that
prevails in
Hi all,
A general coding question: is it better to use return(False) (or 0, or
-1, or whatever) or to raise whateverError("oops")? Are there cases
for each?
--
Have a great day,
Alex (msg sent from GMail website)
mehg...@gmail.com; http://www.facebook.com/mehgcap
_
On Tue, Feb 23, 2010 at 1:55 PM, Giorgio
> And, please let me ask a question: Kent told that nested_namespace(s) are
> default in python 2.6. And i found a line confirming this in py2.6 library.
> But, what about python 2.5 that as you know is the default on linux?
Yes, since 2.2 nested namespaces
Ok Hugo,
so, going back to your example:
def nochange(some_list):
# create shallow copy of list. NOTE: Shallow copies may still bite
you if you change the list members.
some_list = some_list[:]
some_list[1] = 2
Here we've created a new list. It's an object in the global "object-space
:)
On Tue, Feb 23, 2010 at 2:28 PM, Giorgio wrote:
> Thankyou Hugo!
> Ok, so i think the key is of my problem is that when doing X = 0 i'm
> creating a new object, that only exist in the local namespace. BUT, when
> using a list as a parameter for a function i'm only giving it a new name,
> but the o
Thankyou Hugo!
Ok, so i think the key is of my problem is that when doing X = 0 i'm
creating a new object, that only exist in the local namespace. BUT, when
using a list as a parameter for a function i'm only giving it a new name,
but the object it's referring to it's always the same, and is in th
On Tue, Feb 23, 2010 at 1:13 PM, Giorgio wrote:
> I have an update:
> I can easily undertand why this example doesn't work:
> def nochange(x):
> x = 0
> y = 1
> nochange(y)
> print y # Prints out 1
> X is a local variable, and only gets modified in the function, that doesn't
> return any value
Thankyou.
It's clear, this definitely helps me with the first question. But still
doesn't explain almost anything about the example i've posted in the last
post, right?
Giorgio
2010/2/23 Christian Witts
> Giorgio wrote:
>
>> I have an update:
>>
>> I can easily undertand why this example doesn
Giorgio wrote:
I have an update:
I can easily undertand why this example doesn't work:
def nochange(x):
x = 0
y = 1
nochange(y)
print y # Prints out 1
X is a local variable, and only gets modified in the function, that
doesn't return any value.
But it's very difficult for me to underst
I have an update:
I can easily undertand why this example doesn't work:
def nochange(x):
x = 0
y = 1
nochange(y)
print y # Prints out 1
X is a local variable, and only gets modified in the function, that doesn't
return any value.
But it's very difficult for me to understand WHY this works:
Ahah Kent this is amazing.
I was reading the ITALIAN
http://www.python.it/doc/articoli/instpy-0.html version
of that guide that is not updated. But, when i decided to post there i've
posted the link of the guide in english, but actually that's not what i've
readen.
Ok, so in the new python versio
On Mon, Feb 22, 2010 at 9:13 AM, Giorgio wrote:
> And, i have some difficulties understanding the other "strange" example in
> that howto. Just scroll down to: "However, the point is that the value
> of x is picked up from the environment at the time when the function is
> defined. How is this us
Ok, thankyou.
So, in other words, i must pay attention to what i set as default value in a
function. I should NEVER set empty lists as default values.
The guide i've linked says "To learn more about this, you should read the
documentation and look for the difference between *identity* and *equali
On Mon, 22 Feb 2010 03:00:32 am Giorgio wrote:
> Hi,
>
> do you know if there is a way so that i can get multiple values from
> a function?
>
> For example:
>
> def count(a,b):
> c = a + b
> d = a - b
>
> How can I return the value of C and D?
Return a tuple of c and d:
>>> def count(a, b):
...
Hi,
do you know if there is a way so that i can get multiple values from a
function?
For example:
def count(a,b):
c = a + b
d = a - b
How can I return the value of C and D?
Then, i have another question: i've read, some time ago, this guide
http://hetland.org/writing/instant-python.html, ski
Thank you to all who replied. That does help me get a better
idea of all this. I think if I apply a number of the thoughts
expressed I can come to a good, readable re-do of these
longer functions.
Che
_
On 12/8/2009 10:43 PM, Alan Gauld wrote:
While you should not refactor just for the sake of keeping
line-counts, perhaps you should try the small editor approach. Keep
your editor unmaximized, for around 20 lines, around half a screen.
Hmm, I spent the first 10 years of my programming life u
On Tue, Dec 8, 2009 at 4:17 AM, spir wrote:
> Luke Paireepinart dixit:
>
> > I'd say my personal hard-limit for functions before I start refactoring
> is
> > probably around 150-200 lines. But it's rare that functions get that
> long
> > anyway.
>
> !
>
> Aside questions of personal style & tas
"Lie Ryan" wrote
editor screen (or sheet of printout) - when I started that meant
25-60 lines was the range, now you can go up to 60-100 lines if needs
be...
I disagree. I keep my text editor not maximized so I can have multiple
editors open (or multiple splits when using (g)vim); a 100-line
On 12/8/2009 8:27 PM, Alan Gauld wrote:
Remember the old adage that a function should ideally fit on a single
editor screen (or sheet of printout) - when I started that meant
25-60 lines was the range, now you can go up to 60-100 lines if needs be...
I disagree. I keep my text editor not maxim
Luke Paireepinart dixit:
> I'd say my personal hard-limit for functions before I start refactoring is
> probably around 150-200 lines. But it's rare that functions get that long
> anyway.
!
Aside questions of personal style & taste, _I_ could not hold such long funcs.
Far too much to manage f
"Che M" wrote
I have some functions that seem kind of long to me. One of them, with
I realize I can and should refactor parts that are used in other places
in the code, but I don't there are that many in some of these. Is
there a better way to think about organizing this?
The length as s
On Mon, Dec 7, 2009 at 8:37 PM, Che M wrote:
> I have some functions that seem kind of long to me. One of them, with
> white space, comments, print statements, and some commented-out lines,
> is 118 lines long. If I remove all that, it is 57 lines long. I get the
> sense
> that is inappropriate
If your code is not sensitive information, it might help us if you post it
to pastebin or something so we can take a look.
In general though, functions should be as long as they need to be (and no
longer!). 57 lines is not inordinately long. If it's hard for you to read,
though, you should refact
I have some functions that seem kind of long to me. One of them, with
white space, comments, print statements, and some commented-out lines,
is 118 lines long. If I remove all that, it is 57 lines long. I get the sense
that is inappropriately long for a Python function.
The length of it is d
"mbikinyi brat" wrote
Here are two functions printme and change quite spaced apart.
The spacing of the functions inside the source file has nothing
to do with the output.
Python reads the input file, ignores any spacing and executes
any lines it recognises as code.
When executed the o
mbikinyi brat wrote:
Dear all,
Here are two functions printme and change quite spaced apart. When executed the output in bold are joined. What can I do so that the results are separate in two blocks?
?
#Function definition is here
def printme(str):
??? "This prints a passed string into this fun
On Fri, Apr 17, 2009 at 4:42 AM, mbikinyi brat wrote:
> Dear all,
> Here are two functions printme and change quite spaced apart. When executed
> the output in bold are joined. What can I do so that the results are
> separate in two blocks?
The format of the output depends only on the contents of
Dear all,
Here are two functions printme and change quite spaced apart. When executed the
output in bold are joined. What can I do so that the results are separate in
two blocks?
#Function definition is here
def printme(str):
"This prints a passed string into this function"
print str
2009/1/9 Jonathan Balkind :
> Hi tutor list,
> I haven't been programming for long with Python, and I'm currently trying to
> make a simple game using Tkinter. I was wondering whether it is possible to
> submit a function to the mainloop so it will run every time the loop goes
> around? I thought a
Hi tutor list,
I haven't been programming for long with Python, and I'm currently trying to
make a simple game using Tkinter. I was wondering whether it is possible to
submit a function to the mainloop so it will run every time the loop goes
around? I thought about adding the function to the event
On Tue, 18 Apr 2006, Danny Yoo wrote:
> Just as a side note: no semicolons needed. *wink*
Yesterday, I tested something with a two-line perl program. I could not
for the life of me see why I was getting a syntax error. It was only
after 15 minutes of looking up red herrings that it finally d
> New at this but the f(x) with the return statement passes the value back
> to be used in something. The one with the print statement just prints
> it. Correct me if I am wrong experts
> def f(x):
> x = x + 1;
> return x
>
> def g(x):
> x=x + 1;
> print x;
Hi Eric,
Yes, you've got it
Payal Rathod wrote:
On Mon, Apr 17, 2006 at 05:42:05PM +0100, Steve Nelson wrote:
When you define a function, you are writing a block of code which you
can ask to perform a task. The task may be simple, and not require
any additional information, or it may be more complex and need
informati
On Mon, Apr 17, 2006 at 10:31:04AM -0700, Danny Yoo wrote:
> One view that's common is the idea that a function is a box that takes
> an input and returns an output:
Thanks a lot for the detailed help. Well, I have now got atleast basics
of functions, will be doing some more reading on it in nex
Wow! I checked the list at lunchtime and there were only a few things
here, then I check again now and lots of stuff from my tutor! Fortunately
most of it has been answered already - thanks folks - but I feel honour
bound to contribute something...
> What is the difference between,
>
def
This only happens because the python interactive command-line (what you
get when you just type 'python' in a terminal window) prints return
values automatically for you.
If you were executing
f(4)
in a program/script, the return value would be lost.
Paul
Payal Rathod wrote:
> On Mon, Apr 17,
On Mon, 17 Apr 2006, Payal Rathod wrote:
> What is the difference between,
>
def f(x):
> ... return x
> ...
f(4)
> 4
>
def f(x):
> ... print x
> ...
f(4)
> 4
>
> Both give same results.
Clarification. Both "show" the same results from the interpreter. From
what yo
> Sorry, but you have confused me more ;) Can you give an simple example
> of just function() ? Where can it be useful?
>
> And when you say it returns a value, what do you mean by that? return to
> whom and what exactly?
One view that's common is the idea that a function is a box that takes an
On Mon, Apr 17, 2006 at 05:42:05PM +0100, Steve Nelson wrote:
> When you define a function, you are writing a block of code which you
> can ask to perform a task. The task may be simple, and not require
> any additional information, or it may be more complex and need
> information.
What is the di
On Mon, Apr 17, 2006 at 05:10:51PM +0100, Robert Schumann wrote:
> You could say "I kick" (which is like func(), because you're not
> specifying an object to operate on) or your could say "I kick the
> ball" (in which case x = "the ball").
>
Sorry, but you have confused me more ;)
Can you give
Payal Rathod wrote:
> Hi,
> I am now reading Alan's tut on Python, before that I have read a few other
> tuts too.
> But I am not getting functions exactly. I mean what is the difference between,
>
> def func():
>
>
> and
>
> def func(x):
>
>
> When to use which? (please
On Mon, Apr 17, 2006 at 05:02:07PM +0100, Adam wrote:
> The x is a name for a value that you pass in to the function. To call
> the first function you would do
> >>> func()
>
> and the second function:
> >>> func(5) # 5 is just an example it could be any value depending on
> the function.
Sorry b
Sorry - including list.
On 4/17/06, Payal Rathod <[EMAIL PROTECTED]> wrote:
> what is the difference between,
>
> def func():
>
>
> and
>
> def func(x):
>
When you define a function, you are writing a block of code which you
can ask to perform a task. The task may be si
On 17/04/06, Payal Rathod <[EMAIL PROTECTED]> wrote:
> Hi,
> I am now reading Alan's tut on Python, before that I have read a few other
> tuts too.
> But I am not getting functions exactly. I mean what is the difference between,
>
> def func():
>
>
> and
>
> def func(x):
> ...
Hi,
I am now reading Alan's tut on Python, before that I have read a few other tuts
too.
But I am not getting functions exactly. I mean what is the difference between,
def func():
and
def func(x):
When to use which? (please do not say "returns a value" for I do not un
ot; [EMAIL PROTECTED], tutor@python.orgEmne: Re: [Tutor] Functions and random buttonsSimon,> ...I want to make each button clickableThe buttons are clickable so I'm not absolutely sure what you mean?Do you mean you want to add some action to them when they are clicked?Thats done with the
Simon,
> ...I want to make each button clickable
The buttons are clickable so I'm not absolutely sure what you mean?
Do you mean you want to add some action to them when they are clicked?
Thats done with the command option in Tkinter.
define a function and assign it to the button.
In this case
I'm trying just for fun to do a Game of life GUI, and want it to be a random size (later specified through some text field or something).So far it's working, but now I want to make each button clickable, so the button switches between true or false. But I can't find out how to do it. So any help wo
I am greatly impressed by the most useful instruction I have received
since I raised my need for help on this subject. The more I read the
more things fall into place. My grateful thanks to one and all and
please keep the good stuff flowing.
Norman
___
Kent Johnson wrote:
I would use a central dispatcher. Each chapter function could return a
token indicating which chapter is next, or it could return the actual
next chapter function.
Look here for an *extensive* example of this style:
http://homepage.mac.com/spkane/python/paranoia.py
Kent
__
Luke Jordan wrote:
Hi -
I'm working on a command-line game. Is there anything wrong with
having each 'chapter' of the game be a function that links to other
chapters by calling them? I only ask because when a recent traceback
returned about 40 lines worth of error message, I realized that the
fun
Luke,
On Fri, 25 Feb 2005 11:04:11 -0800, Luke Jordan <[EMAIL PROTECTED]> wrote:
> Hi -
>
> I'm working on a command-line game. Is there anything wrong with
> having each 'chapter' of the game be a function that links to other
> chapters by calling them? I only ask because when a recent tracebac
Hi -
I'm working on a command-line game. Is there anything wrong with
having each 'chapter' of the game be a function that links to other
chapters by calling them? I only ask because when a recent traceback
returned about 40 lines worth of error message, I realized that the
functions are all bein
87 matches
Mail list logo