New to Python: Features

Dan Bishop danb_83 at yahoo.com
Tue Oct 5 04:43:18 EDT 2004


Richard Blackwood <richardblackwood at cloudthunder.com> wrote in message news:<mailman.4286.1096945096.5135.python-list at python.org>...
> Hi, I'm new to Python and I'd like to know if Python has the following 
> support: *please answer to each individually, thanks*

Most of your questions are answered at http://docs.python.org/
 
> 1. Multi line comments

No.  But multi-line docstrings are supported.

> 2. Functions as variables:
>     a. functions can be stored in variables, passed as arguments to 
> other functions, and returned as results.

Yes.

>>> def deriv(f, x, delta=1e-8):
...    "Centered difference approximation of f'(x)"
...    return (f(x + delta) - f(x - delta)) / (2 * delta)
...
>>> deriv(lambda x: x * x, 2)
3.9999999756901161

> 3. Function nesting with proper lexical scope (i.e. closures)

Yes.

> 4. Operator overloading (inc. the ability to define new operators)

Yes.  This is done by defining the special methods __add__, __mul__,
etc.

> 5. Can I do this?  print("Hello " .. "World")  --> Hello World

If ".." is supposed to be the string concatenation operator, then yes.

>>> print "Hello " + "World!"
Hello World!

> 6. Constructors

Yes.  They're defined like this:

   class MyClass:
      def __init__(self, arg1, arg2):
         # ...

and called like this:

   x = MyClass(arg1, arg2)

> 8. "Repeat-Until" as in :
>     repeat
>       line = os.read()
>     until line ~= ""
>     print(line)

There isn't a special syntax for posttest loops.  You have to write
them like

   while True:
      line = os.read()
      if not line:
         break
   print line
  
> 10. Can I call an object's method as object:method(arg) and have that 
> translate into object.method(object, arg)

>>> class MyClass:
...    def foo(self, arg):
...       pass
...
>>> m = MyClass()
>>> m.foo(0)
>>> MyClass.foo(m, 0)

> 11. Can I make dynamic statements and nature like with eval() in Javascript?

Yes.

>>> x = 1; y = 2
>>> z = eval("x + y")
>>> exec "print z"
3

> 12. Can I make calls to a function with a varying number of arguments?

Yes.

>>> def func(fixedArgA, fixedArgB, *args):
...    print "called with", 2 + len(args), "arguments"
...
>>> func(1, 2, 3, 4)
called with 4 arguments
>>> func(1, 2, 3, 4, 5, 6)
called with 6 arguments

> 13. Named arguments

Yes.

>>> datetime.date(month=10, day=5, year=2004)
datetime.date(2004, 10, 5)

> 14. Tables with built-in methods for manipulation such as sort, etc.

Try the built-in "list" type.  Also, "dict".

> 15. Table filters

I'm not quite sure what you're asking.  Do you mean something like

>>> [n for n in xrange(2, 20) if isprime(n)]
[2, 3, 5, 7, 11, 13, 17, 19]

?

> 15. Proper Tail Call (otherwise known as Proper Tail Recursion)

No.

> 16. The ability to call a function without brackets

Fortunately, no.

> 17. Is the Python interpreter a JIT? Does it have a bytecode?  Is it as 
> fast as Java?

For the standard interpreter: No. Yes. No.

But try Psyco (psyco.sourceforge.net).

> 21. May modules be stored in variables, passed to and produced from 
> functions, and so forth?

Yes.

>>> import sys
>>> import os
>>> modules = [sys, os]
>>> modules[1].getcwd()
'/home/dan'

> 22. Is the self parameter hidden from me as a programmer?  Can I 
> hide/unhide it as I wish?

No.

> 25. A fully implemented .NET counterpart (I should be able to write 
> Python scripts for both with the same code)

It's not fully implemented yet, but there is a .NET version
(http://www.ironpython.org).

> 33. Function/Method overloading

No, but it can often be simulated by default parameters, or by
"isinstance" checks.

> 34. In pure Python, can I change and add new constructs to the Python 
> syntax?

No.

> 43. Embedding variables in strings like: print "Hello, World. Time: 
> #{Time.now}"

No, but this can be written almost as easily using the % operator.

>>> print "Hello, World.  Time: %s" % datetime.datetime.now()
Hello, World.  Time: 2004-10-05 03:12:14.750796

or

>>> now = datetime.datetime.now()
>>> print "Hello, World.  Time: %(now)s" % locals()
Hello, World.  Time: 2004-10-05 03:14:01.111879

> 44. Case or Switch statements with functionality as such:
> case score
> when 0...40
>    puts "Horrible!"
> when 40...60
>    puts "Poor"
> when 60...80
>    puts "You can do better!"
> when 80...95
>    puts "Now that's acceptable"
> when 95..100
>    puts "That the best you can do?  j/k"
> else
>    puts "Didn't take the test?"
> end

Python does not have a switch/case construct.  Your example would
normally be written as an if...elif...else ladder.



More information about the Python-list mailing list