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
coffeetable
Feb 5, 2006

TELL ME AGAIN HOW GREAT BRITAIN WOULD BE IF IT WAS RULED BY THE MERCILESS JACKBOOT OF PRINCE CHARLES

YES I DO TALK TO PLANTS ACTUALLY

uG posted:

420 write my own string libraries every day

this kind of poo poo is academic programming from high school interns through to senior faculty

Adbot
ADBOT LOVES YOU

Bloody
Mar 3, 2013

that awful moment when you look at your own code from a few months ago and have no loving clue what it does and you didnt write a single comment and you knew full well at the time the sort of atrocities you were committing against your future self but didnt care enough to not commit them

prefect
Sep 11, 2001

No one, Woodhouse.
No one.




Dead Man’s Band

Bloody posted:

that awful moment when you look at your own code from a few months ago and have no loving clue what it does and you didnt write a single comment and you knew full well at the time the sort of atrocities you were committing against your future self but didnt care enough to not commit them

i don't think i've ever written code that i found pleasing to the eye two weeks later

Bloody
Mar 3, 2013

prefect posted:

i don't think i've ever written code that i found pleasing to the eye two weeks later

i dont even know what most of these functions do

hell i even documented one of them and i still dont know what the input data is supposed to be

it doesnt help that its c so the parameters are just pointers to... something.

a cyberpunk goose
May 21, 2007

Bloody posted:

i dont even know what most of these functions do

hell i even documented one of them and i still dont know what the input data is supposed to be

it doesnt help that its c so the parameters are just pointers to... something.

code:

#include "common.h"

#include <stdio.h>

//TODO: Document
void DoNeedful(void*** input, uchar* output, uint32* output_size);

coffeetable
Feb 5, 2006

TELL ME AGAIN HOW GREAT BRITAIN WOULD BE IF IT WAS RULED BY THE MERCILESS JACKBOOT OF PRINCE CHARLES

YES I DO TALK TO PLANTS ACTUALLY

Bloody posted:

i dont even know what most of these functions do

hell i even documented one of them and i still dont know what the input data is supposed to be

it doesnt help that its c so the parameters are just pointers to... something.

post yr code

coffeetable
Feb 5, 2006

TELL ME AGAIN HOW GREAT BRITAIN WOULD BE IF IT WAS RULED BY THE MERCILESS JACKBOOT OF PRINCE CHARLES

YES I DO TALK TO PLANTS ACTUALLY
don't try n clean it up first either

Bloody
Mar 3, 2013

i dont think i legally can unfortunately

prefect
Sep 11, 2001

No one, Woodhouse.
No one.




Dead Man’s Band

Bloody posted:

i dont think i legally can unfortunately

my code is also prohibited by the geneva convention

coffeetable
Feb 5, 2006

TELL ME AGAIN HOW GREAT BRITAIN WOULD BE IF IT WAS RULED BY THE MERCILESS JACKBOOT OF PRINCE CHARLES

YES I DO TALK TO PLANTS ACTUALLY
wrong drat thread

Shaggar
Apr 26, 2006

Bloody posted:

i dont even know what most of these functions do

hell i even documented one of them and i still dont know what the input data is supposed to be

it doesnt help that its c so the parameters are just pointers to... something.

I use java and c# and their general naming conventions so my code is self documenting.

a cyberpunk goose
May 21, 2007

Shaggar posted:

I use java and c# and their general naming conventions so my code is self documenting.

how do you sleep at night

must be hard, knowing there is so much wrong in the world.. if only the ycould see that shaggar is right

uG
Apr 23, 2003

by Ralp

coffeetable posted:

this kind of poo poo is academic programming from high school interns through to senior faculty

lol if you only ever program poo poo that handles all your unicode string poo poo automatically that sounds very exciting

MeruFM
Jul 27, 2010
ascii unicode 8 16 conversion is what I live for, truly I live a king's life

My habit, as a biology turn beep boop person, was to write way too many comments.
I document exactly how code does stuff including each for loop in 6 nested loops
Then I realize I wrote it and feel ashamed, then everyone else can see it too because it's committed.

uG
Apr 23, 2003

by Ralp
never commit, only blame

BONGHITZ
Jan 1, 1970

you're a wizard, array

DONT THREAD ON ME
Oct 1, 2002

by Nyc_Tattoo
Floss Finder

prefect posted:

i don't think i've ever written code that i found pleasing to the eye two weeks later

dont you write perl

a cyberpunk goose
May 21, 2007

chumpchous posted:

dont you write perl

lol

Bloody
Mar 3, 2013

c software update s: everything went better than expected. only took a few hours. tests have been running for as long as development took at this point. the fact that they are still running is a good sign.

AppleCobbler
Feb 8, 2003
remember that time I was just chilling out and definitely not having a massive meltdown? right guys? guys??? :laugh:

MeruFM posted:

ascii unicode 8 16 conversion is what I live for, truly I live a king's life

My habit, as a biology turn beep boop person, was to write way too many comments.
I document exactly how code does stuff including each for loop in 6 nested loops
Then I realize I wrote it and feel ashamed, then everyone else can see it too because it's committed.

Lol 6 nested for loops

Time to refactor bro

Bloody
Mar 3, 2013

AppleCobbler posted:

Lol 6 nested for loops

Time to refactor bro

sometimes you need to exhaustively traverse 6-dimensional space

don't 6d-shame please this is a safe place

prefect
Sep 11, 2001

No one, Woodhouse.
No one.




Dead Man’s Band

chumpchous posted:

dont you write perl

not in the past few years, actually. i miss it :smith:

Greed is eternal
Jun 8, 2008
I ran into this "problem" with Python scoping. I know that Python uses statical scoping but that doesn't really work that well with the lambda keyword.

What does the following program output?
code:
a = []
for i in xrange(5):
	a.append(lambda x: i + x)

print a[1](1)
a) 2
b) 5
c) -1

Answer: b) 5

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord
Python code:
a = [i.__add__ for i in xrange(5)]
I would've done this :smug:

No, I can't explain why lambda is such a piece of poo poo in python other than mumbling "lexical scope and closure or something". My rule of thumb is that if I want to do something fancy like I would in racket it'll go wrong in python.

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord
also there's functools.partial, it never disappointed me unlike loving lambda

qntm
Jun 17, 2009
Python code:
i = 78
def foo():
	print(i)

foo() # "78"

i = 99901

foo() # "99901"
Python code:
i = 78
def foo():
	i += 1 # UnboundLocalError: local variable 'i' referenced before assignment
	print(i)
Python code:
i = 78
def foo():
  global i
  i += 1
  print(i)

foo() # "79"

i = 99901

foo() # "99902"

Symbolic Butt posted:

"lexical scope and closure or something"

prefect
Sep 11, 2001

No one, Woodhouse.
No one.




Dead Man’s Band

qntm posted:

Python code:
i = 78
def foo():
	print(i)
Python code:
i = 78
def foo():
	i += 1 # UnboundLocalError: local variable 'i' referenced before assignment
	print(i)

could somebody explain why the first is okay but the second is bad? :confused:

weird
Jun 4, 2012

by zen death robot
bc Python isn't very good

Bizarro Buddha
Feb 11, 2007
Seriously languages without decent scoping/declaration rules why even bother ffs

prefect
Sep 11, 2001

No one, Woodhouse.
No one.




Dead Man’s Band

Bizarro Buddha posted:

Seriously languages without decent scoping/declaration rules why even bother ffs

i haven't done enough with python to be fully comfortable with it, but i do not like their scoping rules. i understand block scoping; it makes sense to my dumb brain. the curly braces will show you the truth :bahgawd:

Posting Principle
Dec 10, 2011

by Ralp

Symbolic Butt posted:

Python code:
a = [i.__add__ for i in xrange(5)]
I would've done this :smug:

No, I can't explain why lambda is such a piece of poo poo in python other than mumbling "lexical scope and closure or something". My rule of thumb is that if I want to do something fancy like I would in racket it'll go wrong in python.

as usual, c++ is expressive enough to let you define just the closure you need

C++ code:
    vector<function<int(int)>> a;
    for (int i = 0; i < 5; ++i) {
        a.emplace_back([i](int x) {return i + x;});
    }
    for (auto&& func : a) {
        cout << func(1) << endl;
    }
thank you bjarne

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord
oh hey the same thing already posted from 2 pages ago

JewKiller 3000 posted:

code:
>>> def python():
...   this_language = "su"
...   def broken(x):
...     this_language = this_language + x
...   return broken("cks")
... 
>>> python()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 5, in python
  File "<stdin>", line 4, in broken
UnboundLocalError: local variable 'this_language' referenced before assignment



prefect posted:

could somebody explain why the first is okay but the second is bad? :confused:

the way I understand is when you do i += 1 inside a block it's like:

- oh hey is there some name assignment around here? yes. yes there is. gently caress the corresponding nonlocal plebs
- creates new name i
- tries to assign (i + 1) to i
- oops can't evaluate this expression coz there's nothing in i yet!

DONT THREAD ON ME
Oct 1, 2002

by Nyc_Tattoo
Floss Finder

prefect posted:

could somebody explain why the first is okay but the second is bad? :confused:

Wow what the gently caress??

Does it work if you do i = i + 1?

prefect
Sep 11, 2001

No one, Woodhouse.
No one.




Dead Man’s Band

chumpchous posted:

Wow what the gently caress??

sorry. guess i missed a page :sigh:

DONT THREAD ON ME
Oct 1, 2002

by Nyc_Tattoo
Floss Finder

prefect posted:

sorry. guess i missed a page :sigh:

No I'm just amazed

coffeetable
Feb 5, 2006

TELL ME AGAIN HOW GREAT BRITAIN WOULD BE IF IT WAS RULED BY THE MERCILESS JACKBOOT OF PRINCE CHARLES

YES I DO TALK TO PLANTS ACTUALLY

chumpchous posted:

Wow what the gently caress??

Does it work if you do i = i + 1?

http://repl.it/languages

(no, because the problem isn't with the += operator)

e: lotta words if anyone cares

http://stackoverflow.com/questions/370357/python-variable-scope-question

coffeetable fucked around with this message at 16:32 on Jul 18, 2013

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord

chumpchous posted:

Wow what the gently caress??

Does it work if you do i = i + 1?

no

also this returns the same error without any prints:

Python code:
i = 78
def foo():
    print(i)
    i += 1
I think this was the most surprising thing to me: the fact that it apparently looks around for name assignments before doing anything.

PleasingFungus
Oct 10, 2012
idiot asshole bitch who should fuck off
yeah basically that behavior is to discourage people from using/modifying nonlocal variables without being very explicit about it, to help avoid confusion about where exactly symbols are coming from (confusion on the part of maintainers, not the machine)

the same thing happens if you try to assign to a global variable within a function (but lol if you use global variables)

in both cases, python (3, the one true python) provides a way to explicitly specify nonlocal variables that you're going to modify within an inner scope: "nonlocal foo[, bar[, baz...]]"

(or in the case of globals, 'global foo' etc)

it's slightly counterintuitive when you first run into it, but I can see why they did it



re the lambda example: we actually had a thread about closures & such a few months back. might have had a discussion in this very thread, I forget. anyway, if memory serves, you can get the behavior you want by doing something like

code:
a = [(lambda x: lambda y: x + y)(i) for i in xrange(5)]
or more verbosely

code:
def gen_func(x):
   def out_func(y):
       return x + y
a = [gen_func(i) for i in xrange(5)]
which reserves a separate scope to store the value ('i') you want for each generated function



I like closures

I like python

PleasingFungus fucked around with this message at 17:34 on Jul 18, 2013

Malcolm XML
Aug 8, 2009

I always knew it would end like this.

PleasingFungus posted:

yeah basically that behavior is to discourage people from using/modifying nonlocal variables without being very explicit about it, to help avoid confusion about where exactly symbols are coming from (confusion on the part of maintainers, not the machine)

the same thing happens if you try to assign to a global variable within a function (but lol if you use global variables)

in both cases, python (3, the one true python) provides a way to explicitly specify nonlocal variables that you're going to modify within an inner scope: "nonlocal foo[, bar[, baz...]]"

(or in the case of globals, 'global foo' etc)

it's slightly counterintuitive when you first run into it, but I can see why they did it



re the lambda example: we actually had a thread about closures & such a few months back. might have had a discussion in this very thread, I forget. anyway, if memory serves, you can get the behavior you want by doing something like

code:
a = [(lambda x: lambda y: x + y)(i) for i in xrange(5)]
or more verbosely

code:
def gen_func(x):
   def out_func(y):
       return x + y
a = [gen_func(i) for i in xrange(5)]
which reserves a separate scope to store the value ('i') you want for each generated function



I like closures

I like python

Closures are a poor man's object.

Objects are a poor man's closures.

Adbot
ADBOT LOVES YOU

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord

PleasingFungus posted:

code:
a = [(lambda x: lambda y: x + y)(i) for i in xrange(5)]
or more verbosely

code:
def gen_func(x):
   def out_func(y):
       return x + y
a = [gen_func(i) for i in xrange(5)]
which reserves a separate scope to store the value ('i') you want for each generated function

I see what's happening in the case of the named function and when you try to use it but it still baffles me in the case of the lambda, it's like they aren't really anonymous or something.

  • Locked thread