Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Locked thread
Hed
Mar 31, 2004

Fun Shoe
Python 3.5 is out go get some! :toot:

I haven't hosed with async io but I'm looking forward too the new unpacking generalizations

Adbot
ADBOT LOVES YOU

Cingulate
Oct 23, 2012

by Fluffdaddy

Hed posted:

Python 3.5 is out go get some! :toot:

I haven't hosed with async io but I'm looking forward too the new unpacking generalizations
running
code:
conda update --all
from airport wifi here

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

PEP-484 makes tooling (like PyCharm) much better and is now in 3.5 I've been using it on Python 3.4 with PyCharm for a year or so and it's only gotten better.

Quick summary:

484 adds type information to function signatures.

Python code:
def greeting(name: str) -> str:
    return 'Hello ' + name
PyCharm knows that in greeting, the variable name is a string, and it knows that the return value from the function is a string. It will give you warnings when you use the string incorrectly and do code completion on string methods.

You can of course, use your own types:

Python code:
class Tree:
    def my_method(self) -> int:
        return 7

class Bush:
    def trim_bush(self) -> bool:
        return False

def goober(a_tree: Tree) -> Bush:
    # do stuff
    return Bush()
And you can specify sequence types:

Python code:
def wiggedy_wack() -> List[Bush]:
     return [Bush(), Bush(), Bush()]

Dominoes
Sep 20, 2007

I'm glad that made it into the official library; it's nice for documenting complex function arguments like heterogeneous collections.

Cingulate
Oct 23, 2012

by Fluffdaddy
Is there (gonna be) a from __future__ import type_annotations thing? Also why do people continue to use python 2 :colbert:

Proteus Jones
Feb 28, 2013



How do I get 3.5.0 in anaconda. I mean I see it there, taunting me, when I do a 'conda search python', but 3.4.3 is the "latest" version when I do a 'conda update python'.

I've just started using anaconda, and I'm still fumbling around in it.

Dominoes
Sep 20, 2007

flosofl posted:

How do I get 3.5.0 in anaconda?
Wait.

Proteus Jones
Feb 28, 2013




Fair enough. I just wish it wouldn't list it as a result in the conda search like it could actually be installed.

chutwig
May 28, 2001

BURLAP SATCHEL OF CRACKERJACKS

Thermopyle posted:

PEP-484 makes tooling (like PyCharm) much better and is now in 3.5 I've been using it on Python 3.4 with PyCharm for a year or so and it's only gotten better.

I really hope that at some point the interpreter will have a mode to enforce the type signatures if present, like a -t flag or something to that effect.

Cingulate posted:

Is there (gonna be) a from __future__ import type_annotations thing? Also why do people continue to use python 2 :colbert:

For Python 2? It's not getting anything new at this point and adding annotations to 2 would require a lot of work, the type signature stuff builds on the generic annotations that 3 introduced.

As for why 2.x is still used... though most OpenStack stuff has to pass gates at both 2.7 and 3.4 now so it may be usable with 3.x by Mitaka, Twisted seems to be coming along (and a lot of what it does is subsumed into 3.5 anyway with all the async/await functionality), and Mercurial will probably eventually be clubbed into submission by the core Python developers.

QuarkJets
Sep 8, 2008

2.X is still used because it's not compatible with 3.X, and there are a bunch of systems with 2.X dependencies. That's the reason.

Cingulate
Oct 23, 2012

by Fluffdaddy
Wasn't so much a question as a complaint. I'm contributing to a project that's required to run on both 2 and 3 and it makes my life ever so slightly worse.

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

chutwig posted:

I really hope that at some point the interpreter will have a mode to enforce the type signatures if present, like a -t flag or something to that effect.

You can use MyPy to check types for you.

duck monster
Dec 15, 2004

Hed posted:

Python 3.5 is out go get some! :toot:

I haven't hosed with async io but I'm looking forward too the new unpacking generalizations

I still havent evolved from 2.7 yet. I probably should update my thinking. async seems to be the selling point to my line of thinking.

As to why? Lotta legacy, and I've been hacking on python since the late 90s and old dogs/new tricks/etc

Veskit
Mar 2, 2005

I love capitalism!! DM me for the best investing advice!
OK so if anyone has time for a stupid question, I can't wrap my head for how something like this works. I was doing this exercise on for loops and definitions and I can't for the life of me figure out the importance of this exercise. Doesn't having mylist =[numbers] negate the whole function? If you remove it won't this run until infinity? I'm so :psyduck: over functions.

code:
def sum(list):
    sum = 0
    for l in list:
        sum = sum + l
    return sum

mylist = [1,2,3,4,5]
print(sum(mylist))

qntm
Jun 17, 2009

Veskit posted:

OK so if anyone has time for a stupid question, I can't wrap my head for how something like this works. I was doing this exercise on for loops and definitions and I can't for the life of me figure out the importance of this exercise. Doesn't having mylist =[numbers] negate the whole function? If you remove it won't this run until infinity? I'm so :psyduck: over functions.

code:
def sum(list):
    sum = 0
    for l in list:
        sum = sum + l
    return sum

mylist = [1,2,3,4,5]
print(sum(mylist))

Why don't you try that and see?

Space Kablooey
May 6, 2009


I'm not really sure what you are not understanding. Can you explain a bit more in depth?

One thing in the meantime that you can do to hopefully make thing a bit clearer for yourself, is to add a few prints inside the for loop, for example, to print out the l and sum variables.

SurgicalOntologist
Jun 17, 2004

Not sure if this helps your confusion as I can't even tell what your misconception is. But I thought I'd answer your questions literally at least.

Veskit posted:

Doesn't having mylist =[numbers] negate the whole function?

No. All that line does is make a new list and assign it the name "mylist". The only way to "negate" the function would be to have a line like
Python code:
sum = None
Where the name "sum" used to be a function, now it's a None, and the function is lost.

Veskit posted:

If you remove it won't this run until infinity? I'm so :psyduck: over functions.

Try it! If you remove the line mylist = [1,2,3,4,5], you will get an error on the next line, "The name 'mylist' is not defined".

The mylist line is making a list; the next line is passing it to your function. There's nothing unusual or tricky in those last two lines. Completely normal thing to do after you make a function: create a sample input, pass it to the function, and print the result.

sarehu
Apr 20, 2007

(call/cc call/cc)

Veskit posted:

OK so if anyone has time for a stupid question, I can't wrap my head for how something like this works. I was doing this exercise on for loops and definitions and I can't for the life of me figure out the importance of this exercise. Doesn't having mylist =[numbers] negate the whole function? If you remove it won't this run until infinity? I'm so :psyduck: over functions.

I don't know what it means to negate a function. You can answer your questions about what happens if you remove the line mylist = [1,2,3,4,5] by removing the line and seeing what happens:

NameError: name 'mylist' is not defined

It would help us help you if you could more precisely describe what it means for mylist = [numbers] to negate the function, and also, why you expect the program to run until infinity if you removed that line. This reflects some operational misunderstanding of how Python works, but I can't unravel it.

Veskit
Mar 2, 2005

I love capitalism!! DM me for the best investing advice!
Sorry,



if I just write
code:
mylist = [1,2,3,4,5]
print(sum(mylist))
Then I get the answer 15, but if I remove mylist there's nothing for it to reference. I don't understand how the function interacts with mylist or what this lesson is trying to show me.


For reference I was trying to find more info on functions and just used this but I'm so the gently caress confused on what the "lesson" is of it all


http://askpython.com/functions/

Munkeymon
Aug 14, 2003

Motherfucker's got an
armor-piercing crowbar! Rigoddamndicu𝜆ous.



Functions work like stored procedures in SQL*. You pass in arguments (or not) and they do something that's hopefully related to their name and return a value (or not).

*I think you said in the general thread that you had some SQL experience, if not ignore that sentence I guess

sarehu
Apr 20, 2007

(call/cc call/cc)
It seems to me like that web page is designed to explain Pythn 3 to people who are already familiar with programming. Anyway:

1. Why would you want to define this function sum?

The reason is, if you want to add up a list of numbers in multiple parts of your program, you'd have to make a separate for loop every single place you wanted to do that, and it's better to write that code once, in a function.

2. How does it work?

The function definition defines how to compute sum(x), for some value x.

Inside the little world where the function executes, it makes the variable list equal to whatever the function's parameter was. Then the body of the function is run.

It uses a local variable unfortunately named "sum", which might be confusing because it coincidentally is the name of the function. We could pick a different name instead:
code:
def sum(list):
    acc = 0
    for l in list:
        acc = acc + l
    return acc
Try adding a print("the value of list is ", list) statement in the body of the function, and print(acc) and such, to see what's going on.

baka kaba
Jul 19, 2003

PLEASE ASK ME, THE SELF-PROFESSED NO #1 PAUL CATTERMOLE FAN IN THE SOMETHING AWFUL S-CLUB 7 MEGATHREAD, TO NAME A SINGLE SONG BY HIS EXCELLENT NU-METAL SIDE PROJECT, SKUA, AND IF I CAN'T PLEASE TELL ME TO
EAT SHIT

Veskit posted:

Sorry,



if I just write
code:
mylist = [1,2,3,4,5]
print(sum(mylist))
Then I get the answer 15, but if I remove mylist there's nothing for it to reference. I don't understand how the function interacts with mylist or what this lesson is trying to show me.


For reference I was trying to find more info on functions and just used this but I'm so the gently caress confused on what the "lesson" is of it all


http://askpython.com/functions/

Your sum() function takes a list as a parameter - you 'pass it in'. That's what you're doing at the bottom in your main code - you create a list of numbers, and pass it into sum. Sum returns some result, which is what gets printed by the print() function (it gets passed in in the same way)

So what does the sum() function actually do? Well, you defined it up at the top! You pass something in (you're calling it list), and then you iterate over each element in that sequence, adding 1 to your sum each time. Then you return that total as the result

Gothmog1065
May 14, 2009

Veskit posted:

OK so if anyone has time for a stupid question, I can't wrap my head for how something like this works. I was doing this exercise on for loops and definitions and I can't for the life of me figure out the importance of this exercise. Doesn't having mylist =[numbers] negate the whole function? If you remove it won't this run until infinity? I'm so :psyduck: over functions.

Just remember, a function is simply a group of commands that can be run elsewhere in your code. You define a function so you don't have to keep typing that same set of instructions repeatedly.

Your code basically goes like this:

Set mylist to [1,2,3,4,5]
Pass Mylist into the function sum() which was defined above.
Within sum:
Receive a variable (In this case [1,2,3,4,5]) and call it 'list'.
Step over each item of the list, and add each element to the variable 'sum'. This will only run 5 times (as that is how many items are in your list).
return the sum variable back outside of the function
print the returned information.

As for the infinite loop part, you may possible be confusing for and while. For will only do x number of loops (A range or a list for example). While will continue to loop until the while condition is no longer to. (while number < 50: number += 1 #this will run until the variable 'number' is greater than 50, then the while loop will break).

Jewel
May 2, 2009

It's also unfortunate because "sum" is already a builtin function in python. If you remove the def it will still do the same thing which could be confusing. Building on on sarehu's advice, you should test with this code

Python code:
def mySum(list):
    acc= 0
    for l in list:
        acc = acc+ l
    return acc

mylist = [1,2,3,4,5]
result = mySum(mylist)
print(result)

mylist2 = [5,7,1]
result2 = mySum(mylist2)
print(result2)

Veskit
Mar 2, 2005

I love capitalism!! DM me for the best investing advice!

sarehu posted:

It uses a local variable unfortunately named "sum", which might be confusing because it coincidentally is the name of the function. We could pick a different name instead:

Jewel posted:

It's also unfortunate because "sum" is already a builtin function in python. If you remove the def it will still do the same thing which could be confusing. Building on on sarehu's advice, you should test with this code


THIS, WHY DID THEY DO THIS, WHY IN GODS NAME DID THEY DO THIS



So you make the definition, and if you call that definition it'll then run it, and pass it back into wherever you define it.




BUT FOR SOME GOD FORSAKEN REASON THEY USED THE WORD SUM JUST TO CONFUSE THE EVER LIVING gently caress OUT OF ME


code:
def acc(list):
    acc = 0
    for l in list:
        acc = acc + l
    return acc

list=[1,2,3]
print(acc(list))
this prints 6

code:
def acc(list):
    acc = 0
    for l in list:
        acc = acc + l
    return acc

list=[1,2,3]
print(list)
this prints [1,2,3]


That makes way more sense thanks everyone for the help you're all good teachers and don't make me feel too dumb

Veskit fucked around with this message at 19:24 on Sep 14, 2015

baka kaba
Jul 19, 2003

PLEASE ASK ME, THE SELF-PROFESSED NO #1 PAUL CATTERMOLE FAN IN THE SOMETHING AWFUL S-CLUB 7 MEGATHREAD, TO NAME A SINGLE SONG BY HIS EXCELLENT NU-METAL SIDE PROJECT, SKUA, AND IF I CAN'T PLEASE TELL ME TO
EAT SHIT

Veskit posted:

BUT FOR SOME GOD FORSAKEN REASON THEY USED THE WORD SUM JUST TO CONFUSE THE EVER LIVING gently caress OUT OF ME

Let me introduce you to some Java tutorials. Sum sum = new Sum()?

You can think of it like this - there's a function called max() that takes two numbers* and returns the largest one. So wherever you call that function, it'll be replaced by the result when you run the program. This could be super useful if you want to find lots of largest numbers, or if it's a common task that's useful in all kinds of programs (which is why it's part fo the standard library, it's a useful utility).

That's how you use it, but how does it actually work? Well, you define the function by giving it a name and listing some parameters it'll use, and then you write some code that takes those variables and does something with them, and comes up with an answer. A function is just a mini program, and it's a way of handling a task separately and neatly, so you can get the result you want without cluttering up your code with the implementation details, or reinventing the wheel. You don't have to use them, but they can be a nice organisational tool - and at some point you're going to find yourself retyping basically the same code at another point in your program. That's when it's a good idea to break that functionality out, and just say 'do thing with X and Y'

*it can do more than two and they don't even have to be numbers! Another cool thing about functions, you can make them general and versatile, so you can just go 'hey give me the biggest thing', and the details of how that happens are squirreled elsewhere

baka kaba fucked around with this message at 19:43 on Sep 14, 2015

Dominoes
Sep 20, 2007

http://continuum.io/blog/python35

-To use Python 3.5 in Anaconda, use a conda environment:
code:
conda create -n py35 python=3.5
-3.5 will be included in an official Anaconda release at the end of the October.

Proteus Jones
Feb 28, 2013



Dominoes posted:

http://continuum.io/blog/python35

-To use Python 3.5 in Anaconda, use a conda environment:
code:
conda create -n py35 python=3.5
-3.5 will be included in an official Anaconda release at the end of the October.

Kick rear end, thanks. I think setting up an environment is the better way to go anyway, until I'm sure I want 3.5 as the default.

Ekster
Jul 18, 2013

I'm new to Python and I just finished writing my first assembler for an exercise, complete with symbol lookup table. I've never done (serious) text processing before so I feel like I've learned a lot. Here's a simplified version of what I did to get the necessary tokens:

code:
for line in file:
	tokenList = re.split(expression, line)
	tokenList = list(filter(None, tokenList))
	token = tokenList[position]


Everything works fine but I was wondering if there's perhaps a way to skip the filter step? Using re.split() generates a lot of empty entries at different locations depending on the line I feed it.

KICK BAMA KICK
Mar 2, 2009

Veskit posted:

BUT FOR SOME GOD FORSAKEN REASON THEY USED THE WORD SUM JUST TO CONFUSE THE EVER LIVING gently caress OUT OF ME
Doesn't cause a problem here but calling the parameter of that function list is basically the same error -- that's the name of the built-in type. People have actually come in this thread with bugs caused by overwriting a name like that.

Munkeymon
Aug 14, 2003

Motherfucker's got an
armor-piercing crowbar! Rigoddamndicu𝜆ous.



Ekster posted:

I'm new to Python and I just finished writing my first assembler for an exercise, complete with symbol lookup table. I've never done (serious) text processing before so I feel like I've learned a lot. Here's a simplified version of what I did to get the necessary tokens:

code:
for line in file:
	tokenList = re.split(expression, line)
	tokenList = list(filter(None, tokenList))
	token = tokenList[position]


Everything works fine but I was wondering if there's perhaps a way to skip the filter step? Using re.split() generates a lot of empty entries at different locations depending on the line I feed it.

What's the expression look like? You can probably add capture groups to make getting just the parts you want as easy as tokenList.group('name') as seen in https://docs.python.org/3.4/library/re.html#re.match.group

Ekster
Jul 18, 2013

Munkeymon posted:

What's the expression look like? You can probably add capture groups to make getting just the parts you want as easy as tokenList.group('name') as seen in https://docs.python.org/3.4/library/re.html#re.match.group

'[@()=;\s]'

The problem is pretty much \s in practice.

EDIT: Looks like capture groups is what I want, I'll look into it. Thanks!

Ekster fucked around with this message at 23:04 on Sep 14, 2015

Veskit
Mar 2, 2005

I love capitalism!! DM me for the best investing advice!

KICK BAMA KICK posted:

Doesn't cause a problem here but calling the parameter of that function list is basically the same error -- that's the name of the built-in type. People have actually come in this thread with bugs caused by overwriting a name like that.

I've had issues naming something list1. For parameters/variables should you not use integers to help label them?

Dominoes
Sep 20, 2007

Veskit posted:

I've had issues naming something list1. For parameters/variables should you not use integers to help label them?
Be descriptive. For example:

Python code:
def sum(values):

def sum(prices):
Alternatively, per Pep8 (Python style guide), use a trailing underscore in variable names to avoid conflicts with Python keywords.

Dominoes fucked around with this message at 23:21 on Sep 14, 2015

Hadlock
Nov 9, 2004

duck monster posted:

I still havent evolved from 2.7 yet. I probably should update my thinking. async seems to be the selling point to my line of thinking.

As to why? Lotta legacy, and I've been hacking on python since the late 90s and old dogs/new tricks/etc

OpenCV just got official Python 3 support in June of this year. There's enough big legacy projects out there that still require 2.7, and I haven't run in to anything that explicitly requires 3 yet. Async sounds interesting though.

Gothmog1065
May 14, 2009

Veskit posted:

I've had issues naming something list1. For parameters/variables should you not use integers to help label them?

What issue? I use variable1/2/3 all the time (or list_1, list_2, list3 and never had a problem).

KICK BAMA KICK
Mar 2, 2009

Yeah you can't lead with a digit in a name but anywhere else should be fine.

chutwig
May 28, 2001

BURLAP SATCHEL OF CRACKERJACKS

Thermopyle posted:

You can use MyPy to check types for you.

I know MyPy exists, but it'd be even nicer to have something blessed in CPython that says This Is An Okay Thing. Python's obviously survived a quarter century (jfc) without it, but it'd be a nice-to-have thing.

tef
May 30, 2004

-> some l-system crap ->

duck monster posted:

I still havent evolved from 2.7 yet. I probably should update my thinking. async seems to be the selling point to my line of thinking.

As to why? Lotta legacy, and I've been hacking on python since the late 90s and old dogs/new tricks/etc

3.5 for me is the point where using python 3 and rewriting in python 3 seem like a good choice. I dunno if this is true for other people (i do unix plumbing stuff bolting api calls to syscalls).

It's not just the asyncio stuff, it's also the performance improvements over 2.7, and little things like bytestring formatting operations. The third party libraries i rely on are there too! venvs and pips are finally part of the batteries.

Adbot
ADBOT LOVES YOU

QuarkJets
Sep 8, 2008

Hadlock posted:

OpenCV just got official Python 3 support in June of this year. There's enough big legacy projects out there that still require 2.7, and I haven't run in to anything that explicitly requires 3 yet. Async sounds interesting though.

Bingo. OpenCV has been one of my big dependencies, it's just so loving useful and fast for image processing that there's little reason to use anything else.

Opencv is still not part of Anaconda Python 3.4, and I suspect it's a big part of why Enthought hasn't bothered producing a version of Canopy with Python 3.X

  • Locked thread