Re: [Tutor] Quick Pythonic Style Tips

2017-07-24 Thread Abdur-Rahmaan Janhangeer
nice, just wanted some "styling" guide rather than technical guides

you know doing things the python style rather than the python way

Abdur-Rahmaan Janhangeer,
Mauritius
abdurrahmaanjanhangeer.wordpress.com

On 24 Jul 2017 05:09, "Steven D'Aprano"  wrote:

> On Sun, Jul 23, 2017 at 07:02:09PM +0400, Abdur-Rahmaan Janhangeer wrote:
>
> > assert(... is liked by some strongly typed programmers
>
> Not just strongly-typed programmers:
>
> http://import-that.dreamwidth.org/676.html
>
>
> > data encapsulation might be depressing to some migrating coders
>
> "Data encapsulation" does not mean "data hiding". Python has excellent
> data encapsulation:
>
> - packages encapsulate modules together;
>
> - modules encapsulate classes, functions and variables together;
>
> - classes encapsulate object state and behaviour together.
>
>
> Compared to languages like C++ and Java, Python's data HIDING is weak.
> Python has no private, public, protected, etc.
>
> But even in the strictest languages like C++ and Java, there is usually
> some way to "defeat" the compiler and get access to private data and
> break data hiding. For instance, in C++ you can often do something like
>
> #define private public
>
> and in Java you can use reflection. The creator of Python, Guido van
> Rossum, understands that sometimes there *are* good uses for breaking
> data hiding (usually for testing and debugging). Because Python is an
> interpreter where most features are done at runtime rather than compile
> time, implementing data hiding in Python would hurt performance, and
> there would be some way to break it anyway.
>
> So why bother?
>
> Instead, Python is "for consenting adults". Data hiding is very simple:
> the developer flags objects they want to keep private with a leading
> underscore:
>
> _private
>
> and that tells you that this is private and you shouldn't touch it. If
> you decide to ignore this and touch it anyway, then:
>
> - either you have a good reason, and that's okay;
>
> - or you are a "consenting adult", and if your code blows up,
>   well, that's your own fault and don't complain to us.
>
>
>
> --
> Steve
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python3 Help

2017-07-24 Thread Brandon Anderson
Success!

Thank you, Danny!

Mission accomplished.

-Brandon

Sent from my iPhone

On Jul 23, 2017, at 5:44 PM, Danny Yoo  wrote:

>> 2.  I’m trying to locate the directory path to where Python3 is located on 
>> my system, but when I enter
>>the following command:
>>$ type -a python3
>> 
>>I get:
>>-bash: $: command not found
> 
> 
> Ah.  Do not include the leading "$" in the command that you're typing.
> I'm assuming that you're reading some instruction that says something
> like this:
> 
> ---
> 
> Type the following into your command prompt:
> 
>$ type -a python3
> 
> 
> 
> If you are reading such an instruction, don't literally type the
> dollar sign.  "$" is a convention that's used to indicate to you to
> type the rest of the line into your command prompt.  It's because, by
> default, the command shell will use "$" as its primary prompt to tell
> the user that it is ready to accept a new command.
> (https://superuser.com/questions/57575/what-is-the-origin-of-the-unix-dollar-prompt)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python3 Help

2017-07-24 Thread Alan Gauld via Tutor
On 24/07/17 01:58, Alan Gauld via Tutor wrote:

> $ which python3
> 
>>  -bash: $: command not found  
> 
> The $ is the OS prompt you are not supposed to type it in.

While on the subject you might also see something like

# 

Which can mean one of two things
1) It's a comment and you should not type it in
2) It's a root level command and you should su to root
   before running it. (# was the default Unix prompt
   for super users)

The latter usage is dying out and usually replaced with

$ sudo 

Which means that as an ordinary user ($) you type sudo
before the command. sudo should then prompt for your
user password before carrying out the command.

But the older # prompt style is still around in some
onlne tutorials.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] basic decorator question

2017-07-24 Thread bruce
Hi.

I've seen sites discuss decorators, as functions that "wrap" and
return functions.

But, I'm sooo confuzed! My real question though, can a decorator have
multiple internal functions? All the examples I've seen so far have a
single internal function.

And, if a decorator can have multiple internal functions, how would
the calling sequence work?

But as a start, if you have pointers to any really "basic" step by
step sites/examples I can look at, I'd appreciate it. I suspect I'm
getting flumoxed by something simple.

thanks
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] basic decorator question

2017-07-24 Thread Mats Wichmann
On 07/24/2017 08:33 AM, bruce wrote:
> Hi.
> 
> I've seen sites discuss decorators, as functions that "wrap" and
> return functions.
> 
> But, I'm sooo confuzed! My real question though, can a decorator have
> multiple internal functions? All the examples I've seen so far have a
> single internal function.
> 
> And, if a decorator can have multiple internal functions, how would
> the calling sequence work?
> 
> But as a start, if you have pointers to any really "basic" step by
> step sites/examples I can look at, I'd appreciate it. I suspect I'm
> getting flumoxed by something simple.

wrap and return are not two distinct things, they're part of the same
process...  the general concept is that a decorator changes the result
of a function without modifying the function itself by returning a new
function object which does some other stuff in addition to running the
code of the original function object.

This is a really simple wrapper:

def my_decorator(some_function):
def wrapper():
print("Stuff happening before some_function() is called.")
some_function()
print("Stuff after some_function() is called.")
return wrapper

If you have an unwrapped function:

def foo():
print "This is the unwrapped function"

You can show this in action like this:

foo()
bar = my_decorator(foo)
bar()

function names are just handles to the function object, so the middle
line of those three is passing the original function object referred to
by foo to my_decorator, whose inner function returns a function object
which is runs some code before and after the original function.  If the
undecorated fuction does not need to be referred to, the previous often
gets written as:

foo = my_decorator(foo)
foo()

Now to add Python's magical decorator syntax:

@my_decorator
def bar():
print "This is another unwrapped function"

bar()

So all the @my_decorator bit does is provide shorthand for the syntax

bar = my_decorator(bar)

Wasn't ultra-clear on your original question; if you wanted the
"happening before" and "happening after" to call out to other functions
instead of doing a print, you can. Is that what you mean by multiple
internal functions?

Does this clarify at all?

Do hunt some, there are some really good tutorials on decorators.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] basic decorator question

2017-07-24 Thread Steven D'Aprano
Hi Bruce,

On Mon, Jul 24, 2017 at 10:33:25AM -0400, bruce wrote:
> Hi.
> 
> I've seen sites discuss decorators, as functions that "wrap" and
> return functions.
> 
> But, I'm sooo confuzed! My real question though, can a decorator have
> multiple internal functions? All the examples I've seen so far have a
> single internal function.

Yes, a decorator can have multiple internal functions. A decorator is 
just a function, and it can contain anything a function contains. What 
makes it specifically a decorator is what you use it for.

Let's step back and cover a basic: nested functions.

def function(x):
def add_one():
return x + 1
def times_two():
return x*2
return (add_one(), times_two())

function(10)


Can you predict what the result of that will be? I hope you can predict 
that it will return (11, 20). Can you see why?

That sort of nested function isn't very interesting, and you won't see 
much code doing that. But it demonstrates that a function can contain 
multiple inner functions. Now let's look at something that is often 
called a *factory function* -- a function which creates and returns a 
new function.

def factory(a):
def inner(x):
return a + x
return inner  # No parentheses!

add_one = factory(1)  # create a new function & assign it to add_one
add_two = factory(2)

add_one(100)  # returns 101
add_two(100)  # returns 102

How this works isn't important (the technical term is "a closure") but 
the important factor is this: factory() creates a new function, and 
returns it. That function can then be used like any other function 
created with "def".

Functions can not only *return* functions as their return result, but 
they can take functions as arguments. We say that "functions are first 
class values" -- in Python, functions are just another kind of data, 
like ints, floats, strings, lists, dicts and more. The most common 
examples of passing a function as input to another function include:

- builtin functions map() and reduce();

- the key argument to sorted() and list.sort();

- GUI libraries that take callback functions;

- and of course, decorators.


So let's start to create our first decorator. A decorator takes a 
function as argument, "decorates" it in some way (usually by adding some 
sort of extra functionality), and then returns the decorated function. 
Here's a simple example: a decorator that makes the other function print 
a message.

def decorate(func):
def inner(arg):
print('received argument %r' % arg)
return func(arg)
return inner

Now let's set up a couple of functions:

def add_five(x):
return x+5

def add_ten(x):
return x+10

And decorate them:

add_five = decorate(add_five)
add_ten = decorate(add_ten)


Try predicting what 

result = add_five(100)

will print, and what the result will be. Likewise for:

result = add_ten(50)

Try running the code and see if you are correct.


The way I decorated the functions above is a little bit clumsy, so 
Python has a special syntax to make it easier: the "@decorate" syntax.

@decorate
def times_three(x):
return x*3


result = times_three(5)


Can you predict what that will do?


There's more to decorators than that, but hopefully that will 
demonstrate some of the basic concepts. Feel free to ask any more 
questions on the mailing list, and we will answer if we can.


-- 
Steve
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] basic decorator question

2017-07-24 Thread Peter Otten
bruce wrote:

> Hi.
> 
> I've seen sites discuss decorators, as functions that "wrap" and
> return functions.
> 
> But, I'm sooo confuzed! My real question though, can a decorator have
> multiple internal functions? All the examples I've seen so far have a
> single internal function.
> 
> And, if a decorator can have multiple internal functions, how would
> the calling sequence work?
> 
> But as a start, if you have pointers to any really "basic" step by
> step sites/examples I can look at, I'd appreciate it. I suspect I'm
> getting flumoxed by something simple.

Here's a completely useless decorator to drive home the point that

@deco
def f(...):
...

is only a fancy way to write

def f(...):
   ...
f = deco(f)


>>> def fortytwo(func):
... print("I don't care about your function")
... return 42
... 
>>> @fortytwo
... def f():
... print("this is important")
... 
I don't care about your function
>>> f()
Traceback (most recent call last):
  File "", line 1, in 
TypeError: 'int' object is not callable
>>> f
42


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Fwd: Re: basic decorator question

2017-07-24 Thread Alan Gauld
Bah, Forgot to ReplyAll...



 Forwarded Message 

On 24/07/17 15:33, bruce wrote:

> But, I'm sooo confuzed! My real question though, can a decorator have
> multiple internal functions? 

Yes and classes too if you want. A decorator is just a
standard function in all respects. The only thing that makes
it a decorator is that it takes a function as input and returns
a function as a result (ie it observes the decorator protocol).
Internally it can do anything that is legal in any function,
including creating as many inner functions and data structures
and classes as it wants.

> And, if a decorator can have multiple internal functions, how would
> the calling sequence work?

The calling sequence is just like any other code, it starts
at the top and follows the program control logic until it
either hits a return statement of falls off the bottom
(implicitly returning None). Note that the inner functions
do not need to be part of the returned function, they could
just be there to create some fixed value that is then stored
and used in the returned function.

eg:

def f(g):
 def h(): return 42
 myValue = h()
 def z():
   return myValue
 return z

f() is a decorator that takes a function g. It uses an
internal function h() to calculate a value. It then creates
a second inner function z() which returns that value.
Finally the decorator returns z. Notice that neither g()
nor h() are used in the returned function z().

You would use it like so:

def aFunc():
   return 666

always42 = f(aFunc)
print( always42() )

OR

@f
def aFunc() :return 666

print(aFunc())   # --> prints 42 !

HTH
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tutor Digest, Vol 161, Issue 36

2017-07-24 Thread Borisco Bizaro
Please what is the best way to study python programming well.
On Jul 24, 2017 17:00,  wrote:

> Send Tutor mailing list submissions to
> tutor@python.org
>
> To subscribe or unsubscribe via the World Wide Web, visit
> https://mail.python.org/mailman/listinfo/tutor
> or, via email, send a message with subject or body 'help' to
> tutor-requ...@python.org
>
> You can reach the person managing the list at
> tutor-ow...@python.org
>
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of Tutor digest..."
>
> Today's Topics:
>
>1. Re: Python3 Help (Alan Gauld)
>2. basic decorator question (bruce)
>3. Re: basic decorator question (Mats Wichmann)
>
>
> -- Forwarded message --
> From: Alan Gauld 
> To: tutor@python.org
> Cc:
> Bcc:
> Date: Mon, 24 Jul 2017 09:27:18 +0100
> Subject: Re: [Tutor] Python3 Help
> On 24/07/17 01:58, Alan Gauld via Tutor wrote:
>
> > $ which python3
> >
> >>  -bash: $: command not found
> >
> > The $ is the OS prompt you are not supposed to type it in.
>
> While on the subject you might also see something like
>
> # 
>
> Which can mean one of two things
> 1) It's a comment and you should not type it in
> 2) It's a root level command and you should su to root
>before running it. (# was the default Unix prompt
>for super users)
>
> The latter usage is dying out and usually replaced with
>
> $ sudo 
>
> Which means that as an ordinary user ($) you type sudo
> before the command. sudo should then prompt for your
> user password before carrying out the command.
>
> But the older # prompt style is still around in some
> onlne tutorials.
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> http://www.amazon.com/author/alan_gauld
> Follow my photo-blog on Flickr at:
> http://www.flickr.com/photos/alangauldphotos
>
>
>
>
>
> -- Forwarded message --
> From: bruce 
> To: Python Tutor Mailing List 
> Cc:
> Bcc:
> Date: Mon, 24 Jul 2017 10:33:25 -0400
> Subject: [Tutor] basic decorator question
> Hi.
>
> I've seen sites discuss decorators, as functions that "wrap" and
> return functions.
>
> But, I'm sooo confuzed! My real question though, can a decorator have
> multiple internal functions? All the examples I've seen so far have a
> single internal function.
>
> And, if a decorator can have multiple internal functions, how would
> the calling sequence work?
>
> But as a start, if you have pointers to any really "basic" step by
> step sites/examples I can look at, I'd appreciate it. I suspect I'm
> getting flumoxed by something simple.
>
> thanks
>
>
>
> -- Forwarded message --
> From: Mats Wichmann 
> To: bruce , Python Tutor Mailing List <
> tutor@python.org>
> Cc:
> Bcc:
> Date: Mon, 24 Jul 2017 09:49:03 -0600
> Subject: Re: [Tutor] basic decorator question
> On 07/24/2017 08:33 AM, bruce wrote:
> > Hi.
> >
> > I've seen sites discuss decorators, as functions that "wrap" and
> > return functions.
> >
> > But, I'm sooo confuzed! My real question though, can a decorator have
> > multiple internal functions? All the examples I've seen so far have a
> > single internal function.
> >
> > And, if a decorator can have multiple internal functions, how would
> > the calling sequence work?
> >
> > But as a start, if you have pointers to any really "basic" step by
> > step sites/examples I can look at, I'd appreciate it. I suspect I'm
> > getting flumoxed by something simple.
>
> wrap and return are not two distinct things, they're part of the same
> process...  the general concept is that a decorator changes the result
> of a function without modifying the function itself by returning a new
> function object which does some other stuff in addition to running the
> code of the original function object.
>
> This is a really simple wrapper:
>
> def my_decorator(some_function):
> def wrapper():
> print("Stuff happening before some_function() is called.")
> some_function()
> print("Stuff after some_function() is called.")
> return wrapper
>
> If you have an unwrapped function:
>
> def foo():
> print "This is the unwrapped function"
>
> You can show this in action like this:
>
> foo()
> bar = my_decorator(foo)
> bar()
>
> function names are just handles to the function object, so the middle
> line of those three is passing the original function object referred to
> by foo to my_decorator, whose inner function returns a function object
> which is runs some code before and after the original function.  If the
> undecorated fuction does not need to be referred to, the previous often
> gets written as:
>
> foo = my_decorator(foo)
> foo()
>
> Now to add Python's magical decorator syntax:
>
> @my_decorator
> def bar():
> print "This is another unwrapped function"
>
> bar()
>
> So all the @my_decorator bit does is provide shorthand for the syntax
>
> bar = my_decorator(bar)
>
> Wasn't ultra-clear on your origi

Re: [Tutor] new to python

2017-07-24 Thread N6Ghost



On 7/23/2017 1:03 AM, Alan Gauld via Tutor wrote:

On 23/07/17 07:26, N6Ghost wrote:


f = open("C:\coderoot\python3\level1\inputfile.txt", 'r')
for line in file:

Note that you have no variable called 'file'.
So this line doesn't make sense.


  for line in f:
  print(line.rstripe())

This bit will work if you omit the line above and
fix the indentation. (and remove the 'e' from strip()


  f.close()

This should be outside the loop, you don't want
to close the file after every line.

Finally, there is another way to do this which
is considered 'better'/more Pythonic:

with open("C:\coderoot\python3\level1\inputfile.txt", 'r') as f:
  for line in f:
  print(line.strip())

Notice with this construct the closing of the file is
handled for you.


any idea why that does not work?

When posting questions always include the full error text.
Although apparently cryptic it actually contains a lot of
useful detail which saves us from making guesses.



update code:
f = open("C:\coderoot\python3\level1\inputfile.txt", 'r')
for line in f:
for line in f:
print(line.rstripe())

f.close()


C:\coderoot\python3\level1>python secondscript.py
Traceback (most recent call last):
  File "secondscript.py", line 5, in 
print(line.rstripe())
AttributeError: 'str' object has no attribute 'rstripe'




___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] new to python

2017-07-24 Thread Mats Wichmann
On 07/24/2017 04:32 PM, N6Ghost wrote:

> update code:
> f = open("C:\coderoot\python3\level1\inputfile.txt", 'r')
> for line in f:
> for line in f:
> print(line.rstripe())
> 
> f.close()
> 
> 
> C:\coderoot\python3\level1>python secondscript.py
> Traceback (most recent call last):
>   File "secondscript.py", line 5, in 
> print(line.rstripe())
> AttributeError: 'str' object has no attribute 'rstripe'

You presumably meant 'rstrip' (strip from right) rather than 'rstripe'.
With errors like this, look in the documentation to see the available
methods:

https://docs.python.org/3/library/stdtypes.html#string-methods
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor