[ rss / options / help ]
post ]
[ b / iq / g / zoo ] [ e / news / lab ] [ v / nom / pol / eco / emo / 101 / shed ]
[ art / A / beat / boo / com / fat / job / lit / map / mph / poof / £$€¥ / spo / uhu / uni / x / y ] [ * | sfw | o ]
logo
technology

Return ]

Posting mode: Reply
Reply ]
Subject   (reply to 24334)
Message
File  []
close
HG01.jpg
243342433424334
>> No. 24334 Anonymous
26th June 2015
Friday 3:07 am
24334 spacer
I've just started learning Python and wondered if anyone could tell me how to code this memoized function so that it doesn't used an external mem. In javascript this would be done with a closure, can something similar be done in python?

http://pastebin.com/j1pUEvAM
Expand all images.
>> No. 24336 Anonymous
26th June 2015
Friday 8:18 am
24336 spacer
Ha, nice image.
>> No. 24337 Anonymous
26th June 2015
Friday 2:21 pm
24337 spacer
>>24334

Unfortunately variables passed as arguments to functions are non-mutable in python. If you change an argument inside a function it becomes a new variable; the original, passed, variable is not changed outside of the function scope.

Possibly the more 'pythonic' way to do it would be as a class and have fib() as a method working on class data.
>> No. 24338 Anonymous
26th June 2015
Friday 3:24 pm
24338 spacer
>>24337

OK, so I have to define a class for it. I was hoping I could just use "static" keyword or something like in php.
>> No. 24339 Anonymous
26th June 2015
Friday 3:28 pm
24339 spacer
>>24338

PHP is a mish-mash of languages and the 'static' keyword is a holdover from C where static local variables are actually stored in the .data or .bss sections just like a global variable (i.e. not on the stack like a true local variable). In python all variables are objects but their scope is either local or global.
>> No. 24341 Anonymous
27th June 2015
Saturday 2:51 am
24341 spacer
>>24337
This is slightly misleading as it's only immutable types of arguments which trigger that behaviour. The rest are essentially passed by reference and can be modified within a function.
>> No. 24342 Anonymous
27th June 2015
Saturday 5:10 am
24342 spacer
>>24341

Strings and ints are "immutable" by this wonky definition, I'd definitely be interested in knowing what types aren't. Perhaps python 3 behaves differently.
>> No. 24343 Anonymous
27th June 2015
Saturday 1:58 pm
24343 spacer
>>24342
> Strings and ints are "immutable" by this wonky definition

Strings and ints are immutable in python, examples of mutable types are lists (but not tuples) and dicts. You can ignore that as an implementation detail most of the time, but this is one situation where it matters for example.
>> No. 24344 Anonymous
27th June 2015
Saturday 2:25 pm
24344 spacer
>>24343

Yep, you're absolutely right. Mostly I've had ex-C programmers bug me about this while trying to update ints by reference and the idea of suggesting passing them as single-element lists never even crossed my mind. Thanks lad, I've learned something today and I'm only on my first cup of tea.
>> No. 24348 Anonymous
2nd July 2015
Thursday 8:34 pm
24348 spacer
Is there a better way of creating an empty object in Python than this?

http://pastebin.com/psW67T4w

In Javascript you can just do obj = {} or obj = Object.create(null) and go from there.
>> No. 24350 Anonymous
2nd July 2015
Thursday 9:33 pm
24350 spacer
>>24348

You need to think differently about objects in Python, because the language lacks prototypal inheritance. Objects are used very liberally in Javascript, because anything can inherit properties from anything else via the prototype chain; You can add methods as and when you need them, which isn't the case in Python, where you really need to define your classes up front.

The closest thing Python has to an object literal is the dictionary object, but it's much less flexible and shouldn't be thought of as equivalent. Trying to write Python as if it were just Javascript with different syntax is a disaster waiting to happen.
>> No. 24351 Anonymous
2nd July 2015
Thursday 9:55 pm
24351 spacer
>>24348
Could you shed some light on why you want to do that? There's almost never a good reason to create an empty object like that in python (the only exception I've come across so far was when having to write something compatible with optparse and argparse) and as >>24350 points out, trying to force a fundamentally different language's constructs into python will only end in tears.
>> No. 24353 Anonymous
2nd July 2015
Thursday 11:51 pm
24353 spacer
>>24348
https://pypi.python.org/pypi/ostruct

Return ]
whiteline

Delete Post []
Password