Want to read Slashdot from your mobile device? Point it at m.slashdot.org and keep reading!

 



Forgot your password?
typodupeerror
×

Comment average([10, 12.0]) (Score 1) 196

As mentioned in the spec, int list should return an int.

Please clarify the spec further: If the list contains an int and a float (such as average([10, 12.0])), should the result be returning a float or raising a ValueError? I admit that my process of requirements elicitation sounds like I'm asking a lot of nitpicky questions, but it's necessary to avoid an underspecified function. "What Python used to do prior to the addition of true division in 2.2" is still underspecified if the function is ported to any language other than Python.

So your function should return func([-10,-11]) --> (-10-11)/2 = -11 under the rules of integer division.

Until now you have left "the rules of integer division" unspecified. Floor division in Python 2.2+ rounds toward negative infinity, but C integer division rounds toward 0. So you've added to the spec.

Comment average([10, 11]) (Score 1) 196

def average(seq): return sum(seq) / len(seq)

Or in Python 3.4 or later: from statistics import mean as average

Can you implement a simple average() function in Python 3 that satisfies the specifications mentioned above?

I'm not clear what average([-10, -11]) should return under your specification. Or would it raise a ValueError? In general, the average of integers is not an integer but a rational, because integers are not closed under mean. Mean would have to promote to a type capable of representing (or at least approximating) rationals. Python has no built-in exact rational type, but it does have float.

Comment Re:Interoperating with invalid data (Score 1) 196

The program should produce an error AT THE MOMENT IT TRIES TO EXTRACT A Unicode CODE POINT. Not before, and not after.

Which leaves open the question of how best to clean up all the tens of thousands of existing records that may not be valid UTF-8. Otherwise: "This product is unavailable for purchase because its description contains invalid data. This problem has been reported to the store owner."

Comment Deallocating resources when the caller breaks (Score 1) 196

If you are writing an object designed to work with with, you do the cleanup in its __exit__() method function.

So how does the object ensure that every other object that owns it wraps its allocation with with?

sugar can be sweet. I think it's an improvement

I agree with you that with is a syntactic improvement. I was arguing about program semantics that make with necessary, especially when object lifetimes don't correspond directly to program blocks.

You would close the file inside __next__() when you hit end-of-file and right before you raise the StopIteration exception. Am I overlooking something?

You are overlooking the case in which the caller stops using the iterator before the iterator completes, such as a for loop that hits a break statement or an exception not caught within the loop.

Comment [meta] NTS, hypocrisy, and misunderstood words (Score 1) 142

I mentioned that people who call themselves Christians but then commit acts of violence, for flimsy reasons and without provocation, are not in fact practicing Christianity. Some fool cried "hehe I guss there is No True Scotsman then huh?!" while patting himself on the back fiercely.

If someone wrongfully accuses you of creating a no true Scotsman (NTS) fallacy when discussing hypocrisy among self-proclaimed Christians, here's how I'd reply: "I've always defined 'Christian' as someone who follows the teachings of Jesus of Nazareth. Anyone who claims Christianity but materially fails to practice it is something else: a 'hypocrite'. In Jesus's time, there were hypocrites in the leadership of Pharisaic Judaism, and he tore them a new anus in a speech recorded at Matthew 23." Clearly defining the goalposts early on shifts the debate from "you moved the goalposts" to "is this person really practicing?".

This infantile fevered-ego shit is killing Slashdot much faster than a shitty Beta redesign ever could hope to do. It's just far less trendy to protest it.

The NTS fallacy usually has roots in disputes over definitions. Even Scientology recognizes the problems that misunderstood words cause. One can prevent the fallacy by agreeing upon definitions before proceeding, such as "Christian == one practicing Jesus's teachings". This is an anti-NTS step that any Slashdot user can help stop, unlike forced beta for which the only cure is leaving Slashdot in favor of Soylent News or Pipedot. Right now, one can turn off beta, but once Slashdot forces it, the only course of action will be to follow reasoning analogous to Jesus's advice to body integrity identity disorder sufferers in Mark 9:45: "If [Slashdot beta] causes you to stumble, cut it off."

Comment with, try/finally, and long-lived resources (Score 1) 196

One common design pattern for ensuring that unmanaged resources get released is try/finally, which works with JVM, CLR, and CPython, but doesn't work so well with resources held longer than one method.

And this is why it is best practice in Python to use the with statement

This is explicitly a syntactic sugar for the try/finally approach (PEP 343) and shares some of its disadvantages. Like the lifetime of a resource allocated in try and cleaned up in finally, the lifetime of a resource allocated at the start of a with block and cleaned up at the end of said block is limited to the lifetime of that block. If you allocate an object in one method, such as the __init__ method of a class, and retain it, how do you ensure that it gets freed? For example, a class that reads a stream of objects out of a file may implement the iterator protocol: open the file in __init__, return an object in __next__ (next in Python 2), and close the file when?

Comment Chinese or Hindi (Score 1) 196

various internal applications and systems not needing to support more than one language

If a program supports just one language, and that one language is either standard written Chinese or Hindi, then how should it work with even one language without Unicode?

Comment Applications written in Python (Score 1) 196

Then why the hell are they downloading python - a programming language???

To run an application written in that programming language. For the same reason that\ downloads Flash Player to run SWFs and Java to run JARs and a web browser to run web applications, one downloads Python to run Python applications.

Comment Optimize for the hardware you have (Score 1) 196

There's the old parable about two programmers that are told to double the speed of their program. One guy spends a month rewriting the core in assembly and using hardware acceleration. The other guy waits 6 months and buys a new computer.

The second guy failed because the deadline was in three months. Or the second guy failed because other customers running the program on their own computers weren't likewise willing to buy a new computer just to run one program.

Comment VPS (Score 1) 196

If a hosting plan "can support large websites", then it's probably a virtual private server or larger, and you can compile your own Python in your own VPS. Or what am I missing?

Comment In a pickle (Score 1) 196

One problem with import multiprocessing is that it's slow on the operating system on which the majority of desktop applications run. From the manual page that you linked:
  • spawn: Available on Windows, but slow
  • fork: Available on UNIX only
  • forkserver: Available on UNIX only

Another problem is that anything sent through a connection between processes has to be picklable with a bytes representation smaller than about 32 MB, so no passing big images around. Does this cause an actual problem in practice?

Comment import multiprocessing (Score 1) 196

Perl clones the entire interpreter for every thread.

So does Python if you import multiprocessing. But then you run into other problems, such as it being hard to send objects back and forth between processes unless they're small and simple.

Slashdot Top Deals

Kleeneness is next to Godelness.

Working...