Re: Instance method for converting int to str - str() and __str__()

2015-10-03 Thread Laura Creighton
In a message of Fri, 02 Oct 2015 23:35:28 -0700, neubyr writes:
>I was wondering if there is any resource that explains why certain methods
>like str() and type() were implemented the way they are, rather than
>.to_string() or .type() instance/object methods.
>
>I find instance/object methods more intuitive for this cases, but I am
>wondering why it wasn't implemented it like that.
>
>I know there are equivalents like  __str__() and __class__ , but passing
>object to str() or type() seems more popular.
>
>Any resources for reading or some background information would be helpful.
>
>- CS
>
>-- 
>https://mail.python.org/mailman/listinfo/python-list
>

start reading here:
https://www.python.org/download/releases/2.2/descrintro/
and keep searching and reading about type-class unification in Python.

The answer is, because types and classes used to be very different
things, and you _couldn't_.

Laura
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Pyserial and Ubuntu Linux kernel 3.13.0-65-generic

2015-10-03 Thread Laura Creighton
In a message of Fri, 02 Oct 2015 22:36:23 -, Rob Gaddi writes:
>So, this is odd.  I'm running Ubuntu 14.04, and my system did a kernel 
>upgrade from the repository from 3.13.0-63-generic to 3.13.0-65-generic.  
>And pyserial (2.7, installed through pip) stopped working.
>
>Specifically, when I make read() calls on a Serial object, I get the error
>
>serial.serialutil.SerialException: device reports readiness to read but 
>returned no data (device disconnected?)
>
>This comes from the PosixSerial.read() method in serialposix.py, and 
>seems to be a result of the select.select call screwing up.
>
>I reboot under 3.13.0-63-generic.  My code works.  I reboot under 
>3.13.0-65-generic.  My code doesn't.  Implication would seem to be that 
>somehow between these kernel versions, the select() logic in the serial 
>driver changed.  This happens regardless of whether the serial port is 
>real, FTDI USB-UART, or Prolific USB-UART.
>
>Can anyone else confirm?  Also, who do I try to report this one to?
>
>Thanks,
>Rob
>
>-- 
>Rob Gaddi, Highland Technology -- www.highlandtechnology.com
>Email address domain is currently out of order.  See above to fix.
>-- 
>https://mail.python.org/mailman/listinfo/python-list

I think you have this bug
https://bugs.launchpad.net/ubuntu/+source/linux/+bug/460857

or rather, you like the behaviour that ubuntu thinks is buggy,
which never got fixed, and don't like that it changed to be
what ubuntu thinks is correct in .65.

I'd talk to the pyserial issue tracker as the people there ought
to be well aware of this problem, and then see if talking to
ubuntu is the right thing to do.

Laura
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Pyserial and Ubuntu Linux kernel 3.13.0-65-generic

2015-10-03 Thread Laura Creighton
In a message of Sat, 03 Oct 2015 11:07:04 +0200, Laura Creighton writes:
>In a message of Fri, 02 Oct 2015 22:36:23 -, Rob Gaddi writes:
>>So, this is odd.  I'm running Ubuntu 14.04, and my system did a kernel 
>>upgrade from the repository from 3.13.0-63-generic to 3.13.0-65-generic.  
>>And pyserial (2.7, installed through pip) stopped working.
>>
>>Specifically, when I make read() calls on a Serial object, I get the error
>>
>>serial.serialutil.SerialException: device reports readiness to read but 
>>returned no data (device disconnected?)
>>
>>This comes from the PosixSerial.read() method in serialposix.py, and 
>>seems to be a result of the select.select call screwing up.
>>
>>I reboot under 3.13.0-63-generic.  My code works.  I reboot under 
>>3.13.0-65-generic.  My code doesn't.  Implication would seem to be that 
>>somehow between these kernel versions, the select() logic in the serial 
>>driver changed.  This happens regardless of whether the serial port is 
>>real, FTDI USB-UART, or Prolific USB-UART.
>>
>>Can anyone else confirm?  Also, who do I try to report this one to?
>>
>>Thanks,
>>Rob
>>
>>-- 
>>Rob Gaddi, Highland Technology -- www.highlandtechnology.com
>>Email address domain is currently out of order.  See above to fix.
>>-- 
>>https://mail.python.org/mailman/listinfo/python-list
>
>I think you have this bug
>https://bugs.launchpad.net/ubuntu/+source/linux/+bug/460857
>
>or rather, you like the behaviour that ubuntu thinks is buggy,
>which never got fixed, and don't like that it changed to be
>what ubuntu thinks is correct in .65.
>
>I'd talk to the pyserial issue tracker as the people there ought
>to be well aware of this problem, and then see if talking to
>ubuntu is the right thing to do.
>
>Laura
>-- 
>https://mail.python.org/mailman/listinfo/python-list

I think I said that poorly.

What I think happened is that ubuntu made some changes to fix this
problem, and along the way they managed to break things for you,
and maybe all pyserial users.  But I would talk to the pyserial people
about that.

Laura
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Pyserial and Ubuntu Linux kernel 3.13.0-65-generic

2015-10-03 Thread Laura Creighton
With better searching, I find this bug.
https://bugs.launchpad.net/ubuntu/+source/linux-lts-trusty/+bug/1501345

Looks like that's the real one.

Laura
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Instance method for converting int to str - str() and __str__()

2015-10-03 Thread Terry Reedy

On 10/3/2015 2:35 AM, neubyr wrote:


I was wondering if there is any resource that explains why certain
methods like str() and type()


These are classes.  Calling a class calls the class construction and 
initialization functions.  These return an instance of the class.
While reading the tutorial, read the library reference, chapter 2. 
Chapter 4 is also extremely important to read.


--
Terry Jan Reedy

--
https://mail.python.org/mailman/listinfo/python-list


Re: Check if a given value is out of certain range

2015-10-03 Thread Steven D'Aprano
On Fri, 2 Oct 2015 07:45 am, John Gordon wrote:

> In <87r3le1ht3@elektro.pacujo.net> Marko Rauhamaa 
> writes:
[...]
>> Wouldn't
> 
>>x < 0 or 10 < x
> 
>> be even more visual?
> 
> I don't know what you mean by "more visual".
> 
> In my opinion, when comparing a variable to a constant, it's more natural
> to have the variable on the left and the constant on the right, so that's
> one strike against this code.
> 
> Another strike is that the code isn't consistent with itself; it puts the
> variable on the left in the first comparison, then swaps to the right for
> the second comparison.

The alternative is equally inconsistent: for one comparison it uses less
than, for the other it uses greater than.


I find this discussion about the relative readability of 

not 0 <= x <= 10  #1

versus 

0 < x or x > 10  #2
0 < x or 10 < x

to be a good example of people's propensity to invent so-called "rational"
justifications for irrational preferences. Honestly, all these people
claiming that reading #1 takes them "slightly more effort" than reading #2,
or vice versa. Really? You've done an objective test of this? I don't think
so.

We know from UI testing in other fields that people's *self-reported*
efficiency and their *actual* efficiency are almost completely
uncorrelated. Back in the DOS versus Macintosh days, when mice were new,
DOS users consistently reported that using a mouse was too slow and
inefficient compared to the keyboard, but actual objective measurements of
the time that they took to do tasks showed the opposite.

E.g. subjects were asked to do some task, using the keyboard and then again
using the mouse, and (let's say) it actually took them 3 minutes to do it
via keyboard and 2 minutes via mouse, they reported that using the keyboard
commands was much more efficient, fast and easy.

Whether you have #1 or either variation of #2, the time it takes to read and
comprehend the expression is likely to be of the order of a few dozen
milliseconds, plus or minus a few dozen milliseconds. I expect the
variation will be nearly as great as the average time. (But of course, I
haven't done objective studies either.)

Anyone who has done high-school level maths should be familiar with the
notation `0 <= x <= 10`, read as "x between 0 and 10". (Anyone who is not
can hardly claim to be fluent in Python -- that's like claiming to be
fluent in English while not knowing where and how to use an exclamation
mark.)

If you want to check for the opposite, x not between 0 and 10, the natural
way to do so is with `not`: `not 0 <= x <= 10`. If you prefer `x < 0 or x >
10` for whatever reason, that's fine too. Some people prefer pizza, some
prefer kebabs... inventing (spurious) arguments of (imaginary) efficiency
("It takes me half a second less time to chew each mouthful of burger
compared to kebab") is unnecessary. But we do love the argument from
efficiency.

Frankly, as a profession, we programmers are lousy at prioritising. We write
pages and pages of convoluted, inconsistent, hard to understand code that
can sometimes take hours or even days to comprehend well enough to make
even a minor change, while spending dozens of person-hours arguing about
which expression saves them a microsecond or two of reading time. Yay us!



-- 
Steven

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Check if a given value is out of certain range

2015-10-03 Thread Bartc

On 03/10/2015 11:29, Steven D'Aprano wrote:

On Fri, 2 Oct 2015 07:45 am, John Gordon wrote:



I find this discussion about the relative readability of

not 0 <= x <= 10  #1

versus

0 < x or x > 10  #2
0 < x or 10 < x

to be a good example of people's propensity to invent so-called "rational"
justifications for irrational preferences. Honestly, all these people
claiming that reading #1 takes them "slightly more effort" than reading #2,
or vice versa. Really?


I find #1 straightforward (although I'm bothered by the lack of 
parentheses as I would parse that as (not 0). Presumably, this does 
actually mean not (0<=x<=10).)


But I spent a minute looking at #2, and I'm still not sure that it 
expresses the same thing. I would have written #2 as:


 x<0 or x>10

--
Bartc
--
https://mail.python.org/mailman/listinfo/python-list


Re: Instance method for converting int to str - str() and __str__()

2015-10-03 Thread Steven D'Aprano
On Sat, 3 Oct 2015 04:35 pm, neubyr wrote:

> I was wondering if there is any resource that explains why certain methods
> like str() and type() were implemented the way they are, rather than
> .to_string() or .type() instance/object methods.

There is a FAQ that might help with this question:

https://docs.python.org/2/faq/design.html

In early Python, some objects (like ints, floats, tuples, None) had no
methods at all, so you couldn't have None.to_string() and expect it to
work. There was no base class for the entire object hierarchy, so no method
they could inherit: every class would have to re-implement their own
to_string method.

But even today, when all objects have methods and all inherit from object,
having functions instead of methods for some tasks makes good sense. It
allows us to write functions that operate by a protocol, rather than purely
by inheritance, and it helps guarantee consistent naming.

For example, let's look at conversion to bool. Should that method be
called .bool, .boolean, .to_bool, .truthify, ... ? Different classes that
don't inherit from each other may make different choices. By having a
built-in function (technically, a class, but the difference doesn't matter
here) called bool, that ensures one standard way to convert to bool.

How does bool() work? As a built-in function, it can (in theory) include
optimizations that aren't available to a method, for example it could look
like this pseudocode:

def bool(obj):
if obj is None: return False
if obj is a number: return obj != 0
...

which may be faster than calling obj.bool() since it doesn't have to search
the inheritance chain (remember that method resolution in Python happens at
runtime, not compile-time).

Of course, the built-in bool needs to know how to deal with custom classes
that aren't built-in. For that, we have a *protocol* that tells the
built-in functions how to deal with new, unknown classes. The most basic
protocol is to call a specified "dunder" method. Dunder stands for:

Double leading and trailing UNDERscores

and refers to those methods __str__, __add__, __radd__, etc. that you so
often see. All dunder names are reserved for use by Python, so you should
never use them yourself, and especially never invent your own.

bool() is a good example of a protocol because it is more complex than just
a single method call. It looks something like this:

# Python 3 version
def bool(x):
# possible optimizations for built-ins here...
# now deal with everything else
T = type(x)
if hasattr(T, '__bool__'):
flag = T.__bool__()
if flag in (True, False):
return flag
else:
raise TypeError
elif hasattr(T, '__len__'):
return T.__len__() != 0
else:
return True


bool(x) ignores methods defined on the instance x itself, and goes straight
to the class object (saving a runtime lookup). If the dunder method does
not return an actual boolean, it raises an error. And if the object doesn't
define a __bool__ method, it falls back on __len__, and if it doesn't
define __len__ either, it could in principle have other fallbacks, but in
practice just returns a default value.

(Of course, the actual built-in bool may be written in C, not Python, but
that's besides the point.)

So you should never call dunder methods such as __str__ yourself, always
call the wrapper. Chances are very good that the function form will contain
optimizations that are unavailable to you, will perform pre-processing or
post-processing, catch exceptions, or error-check the result.




-- 
Steven

-- 
https://mail.python.org/mailman/listinfo/python-list


Footnotes in ReST

2015-10-03 Thread Steven D'Aprano
I have a document written in Restructured Text format, and I use lots of
footnotes:

blah blah blah [1]_ and blah blah blah [2]_.
blah blah [3]_ blah ... blah blah
blah blah [999]_.

.. [1] fe
.. [2] fi
.. [3] fo
   ...
.. [999] fum


I need to add a footnote between [2] and [3], but I don't want to have to
renumber the following 997 footnotes by hand. Is there something I can do,
within the syntax of ReST itself, to help?


-- 
Steven

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Footnotes in ReST

2015-10-03 Thread Chris Angelico
On Sat, Oct 3, 2015 at 9:39 PM, Steven D'Aprano  wrote:
> I need to add a footnote between [2] and [3], but I don't want to have to
> renumber the following 997 footnotes by hand. Is there something I can do,
> within the syntax of ReST itself, to help?

Now that they're all numbered manually? Not sure. But if you know in
advance you're going to have that kind of thing, there are other ways
to do footnotes:

https://hg.python.org/peps/file/tip/pep-0498.txt
https://www.python.org/dev/peps/pep-0498/

Otherwise, I'd look at scripting the 997-footnote changes. It
shouldn't be too hard to script - will probably take only twice as
long as doing it manually would have...

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Check if a given value is out of certain range

2015-10-03 Thread Laura Creighton
Actually, the fact that adults have more difficulty processing
negations is one of the earliest things proven experimentally
in experimental psychology.

Clark, H., & Chase, W. (1972). On the process of comparing sentences against 
pictures. Cognitive Psychology, 3, 472–517.

is one of the most heavily cited experiments in the field.

The question is _why_?

The early assumption was that the negation is conceptually more
difficult.  People think logically, and a negation is a harder
sentence to construct.

Recently, this idea has come to be questioned:
see
http://www.google.se/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&ved=0CB0QFjAAahUKEwisvv_6lKbIAhXo8XIKHX32BKE&url=http%3A%2F%2Flangcog.stanford.edu%2Fpapers_new%2Fnordmeyer-2015-underrev.pdf&usg=AFQjCNEaPmluLtZs6aeAHgnyCCLEDazJdg

which I shortened to:
http://bit.ly/1OOfK46

It seems that people tend to hear what they expect to hear, and that
effect overwhelms any 'this is harder to constuct logically' effect.

Also, the 'mouse is quicker' experiment was rather more nuanced than
you reported.  It seems when measuring 'what is faster' the cognitive
load is important.  Time seems to go faster when you are thinking
harder about something, than when you are not.  (i.e. how did my
coffee get cold?  I just started hacking on this thing?)  It seems
that mouse editing was less of a cognitive load than typing, for
the people studied, thus they reported that keyboard was faster,
when it wasn't for them.

But

the experiment was replicated among secretaries who had superior typing
skills (and used emacs a lot).  For a lot of them, moving text by mouse
was not faster than using keyboard macros.  But the limiting factor
seemed to be 'how hard did I have to think about it' -- if you move
things by keyboard macro without thinking, then you won't report that
it takes you time, and you won't accurately report how long it takes
to do the task.

Thus for this experiment, the conclusion was that people were a whole
lot better at telling 'how hard did this make me think' than 'how
long did it take me to do the job'.  

Also, when you changed the requirements to 'reformat text all day long'
a huge number of people could get a lot more work done using emacs than
using the mouse editor.

We ran  both of these experiemnts in the coputer graphics lab at the
University of Toronto as a joint csc/psych exp in the mid 1980s, and
our conclusion at the time was that, for 'all day long' sorts of
tasks what was most important was boredom.  Once the subject got
bored with the task, it took longer.

For me the experiment was wonderful for another reason.  The
University of Toronto has long believed that their secretaries weren't
intelligent enough to use emacs, and had gone to particular effort to
design an easier editor for them to use.  But the secretaries of
the Political Science department, who used to come down to the
department of Zoology (where I worked) to use our pdp-11/44 for
text formating, and me (the undergraduate they hired to help
Zoologists format their graduate theses prior to having them
phototypeset) for advice, once saw me fix a problem by writing an
emacs macro, and hauling their document into emacs and using it.

They were entranced.  And wondered if I could teach them how to do it.
I thought, fine enough, and so taught classes in how to use emacs to
about 6 of them for 3 nights a week one summer.

And it turned out that everybody, including one sweet lady whose powers
of reasoning was seriously sub par, learned how to use emacs.  She learned
mostly by rote, but she could do it.  And the first two weeks of learning
how to write macros was very hard.  I learned a lot about how not to
teach things in that class. :)  So a good bit of the problem was lack
of skill in the teacher.

But once we started on writing them for real life tasks they had, and
not useless contrived examples I came up with, it went a lot better.
And soon they were quite proficient, and Political Science had its own
set of common emacs macros.

I got a real understanding of how much 'retype it by hand, it is
faster' you do, if your normal rate of typing is 165 wpm or more.
Especially if you grew up with paper, not computers, where retyping it
was considered very normal ...

At any rate when I heard about this experiment I got the secretaries
to compete, for fun.  The secretaries with their own emacs macros beat
the heck out of everybody else at 'copying and moving text around',
which was, after all, something they did all of the time.

The researchers thought that this wasn't a fair test, as the playing
field was so far from level.  The secretaries thought they could
better their score if they had been given the task the night before --
as they would have designed an even better emacs macro.  And, of course,
if they had a lot of that particular task to do, they would have
'had Caroline write one and teach it to us', as was usual.

Which got people down to the heart of the matter.  People wanted to
measur

Re: Footnotes in ReST

2015-10-03 Thread Laura Creighton
In a message of Sat, 03 Oct 2015 21:39:26 +1000, "Steven D'Aprano" writes:
>I have a document written in Restructured Text format, and I use lots of
>footnotes:
>
>blah blah blah [1]_ and blah blah blah [2]_.
>blah blah [3]_ blah ... blah blah
>blah blah [999]_.
>
>.. [1] fe
>.. [2] fi
>.. [3] fo
>   ...
>.. [999] fum
>
>
>I need to add a footnote between [2] and [3], but I don't want to have to
>renumber the following 997 footnotes by hand. Is there something I can do,
>within the syntax of ReST itself, to help?
>
>
>-- 
>Steven

You shouldn't have numbered them manually in the first place.
Use '#' instead to get automatic numbered footnotes.
http://docutils.sourceforge.net/docs/ref/rst/restructuredtext.html#footnote-references

Laura
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Check if a given value is out of certain range

2015-10-03 Thread Steven D'Aprano
On Sat, 3 Oct 2015 10:12 pm, Laura Creighton wrote:

> People think logically


LOL :-)



-- 
Steven

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Footnotes in ReST

2015-10-03 Thread Steven D'Aprano
On Sat, 3 Oct 2015 10:21 pm, Laura Creighton wrote:

> In a message of Sat, 03 Oct 2015 21:39:26 +1000, "Steven D'Aprano" writes:
>>I have a document written in Restructured Text format, and I use lots of
>>footnotes:
[...]
> You shouldn't have numbered them manually in the first place.
> Use '#' instead to get automatic numbered footnotes.
>
http://docutils.sourceforge.net/docs/ref/rst/restructuredtext.html#footnote-references

Thanks.

Especially useful is the ability to label footnotes so that they can be
auto-numbered while still being able to refer to them from multiple places:


If [#note]_ is the first footnote reference, it will show up as
"[1]".  We can refer to it again as [#note]_ and again see
"[1]".  We can also refer to it as note_ (an ordinary internal
hyperlink reference).

.. [#note] This is the footnote labeled "note".


That's the problem I was trying to solve by manual numbering.



-- 
Steven

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Pyserial and Ubuntu Linux kernel 3.13.0-65-generic

2015-10-03 Thread Michael Torrie
On 10/03/2015 03:19 AM, Laura Creighton wrote:
> With better searching, I find this bug.
> https://bugs.launchpad.net/ubuntu/+source/linux-lts-trusty/+bug/1501345
> 
> Looks like that's the real one.

This ubuntu bug and the other bug you mention seem to be about FTDI
devices. Rob said in his original post that the problem occurs with a
real UART serial port as well, and also the prolific USB chipset.

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Newbie: Designer Looking to Build Graphics Editor (PS/AI)

2015-10-03 Thread Michael Torrie
On 10/02/2015 02:23 PM, Kenneth L wrote:
> No don't tell me what to do. I joined the military 3 years ago. You
> wouldn't believe the stuff I wasn't able to do before but now I am.
> You can keep your advice to yourself. I wasn't asking for something
> simple. I was asking for a starting point. The 3d was to show you
> I've learned hard stuff and it didn't scare me. I was building web
> pages when I was in grade school late 90s. I had no idea how to build
> an iPhone app but I created one, lol.
> 
> I want to build a graphics program or even a prototype. Maybe I can
> pass it over to a expert and hire then to build it. I have a dozen
> degree and certificates on my wall. And I didn't get that by going
> basic. Keep your advice Bartc.

No one is insulting your intelligence, so there's no need to get snippy.
It's easy to take offense in an environment like this as things sound
much harsher in our own heads than they were likely intended.

The reason that you're not getting the responses you want is because
what you are asking for involves so many different things it's hard to
know how to respond. I have no doubt you may be able to do much of what
you want, but it will take a lot of time and a lot of googling of
various things.  Sounds like you need to search for information concerning:
- Python programming in general (tutorials, mini courses, etc)
- GUI programming in Python. There are many options here including
tkinter, PyQt (or PySide), PyGObject (GTK+ 3.x), and also Kivy.
- Vector drawing libraries (PyQt has support, Cairo is another library,
maybe a python-based svg library)
- bitmap graphics engine of some kind, probably GEGL, which has Python
bindings
- linear algebra

I'm sure you can learn all this, but you aren't going to be able to come
here and simply ask about how to put everything together: there's no
turn-key solution we can point you to.  Fire up your browser and start
learning about each of these things.

Take heart, though.  One man created this all by himself using the C#
language:
http://www.getpaint.net/index.html

Tearing down the GIMP is an excellent idea, though that will also be
very daunting.  GIMP is a huge program, written in C. On the subject of
GIMP, I find it interesting you dismissed it out of hand. GIMP is
certainly deficient in many areas, but to say you can't use it because
it's so different from Photoshop strikes me as, well, a cop out.
Photoshop is intuitive because you learned it.  As you are fond of
talking about how much you enjoy learning, so learn how to use the GIMP,
and as Laura said, hack on it using Python to make add-ons.

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Pyserial and Ubuntu Linux kernel 3.13.0-65-generic

2015-10-03 Thread Laura Creighton
In a message of Sat, 03 Oct 2015 08:38:53 -0600, Michael Torrie writes:
>On 10/03/2015 03:19 AM, Laura Creighton wrote:
>> With better searching, I find this bug.
>> https://bugs.launchpad.net/ubuntu/+source/linux-lts-trusty/+bug/1501345
>> 
>> Looks like that's the real one.
>
>This ubuntu bug and the other bug you mention seem to be about FTDI
>devices. Rob said in his original post that the problem occurs with a
>real UART serial port as well, and also the prolific USB chipset.
>
>-- 
>https://mail.python.org/mailman/listinfo/python-list

 https://bugs.launchpad.net/ubuntu/+source/linux-lts-trusty/+bug/1501345
 seems to be about a whole lot of serial ports to me, not just FTDI

Laura
-- 
https://mail.python.org/mailman/listinfo/python-list


function code snippet that has function calls I have never seen before. How does it work.

2015-10-03 Thread Ronald Cosentino
def funA(x,y,z):
return (x+y) * z
def funB(x,y):
return(x-y)
print(funA(4,funB(2,3), funB(3,2)))

the answer is 3. I don't know how it works.

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: function code snippet that has function calls I have never seen before. How does it work.

2015-10-03 Thread Joel Goldstick
On Sat, Oct 3, 2015 at 1:40 PM, Ronald Cosentino 
wrote:

> def funA(x,y,z):
> return (x+y) * z
>
The above takes 3 values and returns a value


> def funB(x,y):
> return(x-y)
>
The above takes 2 values and returns a value


> print(funA(4,funB(2,3), funB(3,2)))
>

you are printing the result of funA, to which you are passing 4, the value
returned by funB(2,3) and the value returned by funB(3,2)

So, print(funA(4, -1, 1))

Which is 4 + -1 * 1

Which is 3 * 1


>
> the answer is 3. I don't know how it works.
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>



-- 
Joel Goldstick
http://joelgoldstick.com
-- 
https://mail.python.org/mailman/listinfo/python-list


RE: function code snippet that has function calls I have never seen before. How does it work.

2015-10-03 Thread Joseph Lee
Hi Ronald,
Answers inline.

-Original Message-
From: Python-list
[mailto:python-list-bounces+joseph.lee22590=gmail@python.org] On Behalf
Of Ronald Cosentino
Sent: Saturday, October 3, 2015 10:41 AM
To: python-list@python.org
Subject: function code snippet that has function calls I have never seen
before. How does it work.

def funA(x,y,z):
return (x+y) * z
def funB(x,y):
return(x-y)
print(funA(4,funB(2,3), funB(3,2)))

the answer is 3. I don't know how it works.

JL: Okay, let's step through the print routine.

1. Before print does its job, the function result will be gathered.
2. The argument to print is result of funA, which itself takes result of two
calls to FunB.
3. First, results of funB calls will be gathered (subtracting 3 from 2 and 2
from 3, respectively). So it becomes:
print(funA(4, (2-3), (3-2)))
4. Then funA will continue to perform its job, returning the result for
output. Thus the final expression that print will print is:
print((4+-1) * 1)

When looking at a function that takes result of another function, it is
important to look at what the inner callee does (in this case, look at what
funB does first).
Cheers,
Joseph



-- 
https://mail.python.org/mailman/listinfo/python-list

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: function code snippet that has function calls I have never seen before. How does it work.

2015-10-03 Thread Ervin Hegedüs
hi,

On Sat, Oct 03, 2015 at 10:40:57AM -0700, Ronald Cosentino wrote:
> def funA(x,y,z):
> return (x+y) * z
> def funB(x,y):
> return(x-y)
> print(funA(4,funB(2,3), funB(3,2)))
> 
> the answer is 3. I don't know how it works.

it's simple:

- there is a "composition of functions", generally
  f(g()) (function in argument list of another function)
- first, Python evaulates the arguments first, from left to right 
- in this point, you'll get -1 for 2nd arg, and 1 for 3rd arg
- then your funcA() will be called with these arguents:
  4, -1, 1
- funcA() calculates this:
  (x+y)*z, in this case (4+(-1))*1


which is 3...


a.


-- 
I � UTF-8
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: function code snippet that has function calls I have never seen before. How does it work.

2015-10-03 Thread Denis McMahon
On Sat, 03 Oct 2015 10:40:57 -0700, Ronald Cosentino wrote:

> def funA(x,y,z):
> return (x+y) * z
> def funB(x,y):
> return(x-y)
> print(funA(4,funB(2,3), funB(3,2)))
> 
> the answer is 3. I don't know how it works.

def funA(x, y, z):
return (x+y) * z

def funB(x, y):
return (x-y)

# this line
# print(funA(4,funB(2,3), funB(3,2)))
# can be written as the following 4 lines:

a = funB(2, 3) # 2 - 3 -> -1

b = funB(3, 2) # 3 - 2 -> 1

c = funA(4, a, b) # (4 + -1) * 1 -> 3

print(c) # 3

-- 
Denis McMahon, denismfmcma...@gmail.com
-- 
https://mail.python.org/mailman/listinfo/python-list