New to Python: Features

Josiah Carlson jcarlson at uci.edu
Tue Oct 5 06:05:08 CEST 2004


Had you read the tutorial, you would discover the answers to all of
these questions, but I'll answer them anyways.


> 1. Multi line comments

No.

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

Yes.

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

Yes.

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

Yes, check the operator module for names of operations.

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

Not precisely.  You can do:
print "Hello","World"
print ("Hello " "World")
print "Hello "+"World"

and many other variants.

> 6. Constructors

If you mean class instantiation, yes.

> 7. "Chunks": as in a code block contained within a string, file, or 
> delimited by some sort of notation (such as brackets) which can be 
> passed to and from functions, stored in objects, with the option of 
> local scoping of variables declared within it.

code = "print 'hello world'"
glbls = {}
lcls = {}

exec code, glbls, lcls


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



line = sys.stdin.readline()
while line != '':
    print line
    line = sys.stdin.readline()

> 9. Generic for loops where " for i=1,f(x) do print(i) end" would print i 
> only once.

for i in xrange(1,n+1,1):
    print i
    break


> 10. Can I call an object's method as object:method(arg) and have that 
> translate into object.method(object, arg)

class foo:
    def goo(self, arg):
        print self, arg
    def bar(arg):
        print arg
    bar = staticmethod(bar)

a = foo()
a.goo(arg) #will print information about the instance and argument
a.bar(arg) #will only print information about the argument

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

Yes, check both eval and exec

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

Yes.

> 13. Named arguments

Yes.

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

We call them lists.

> 15. Table filters

filter(callable, iterable)

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

No, it kills tracebacks.

> 16. The ability to call a function without brackets

No.

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

There is a module called Psyco that does do JIT compilation.
In the CPython implementation, bytecode is automatically generated.

If you want to know about speed, check a benchmark site.


> 18. The ability to modify the import/require functionality (how modules 
> can be loaded)

Yes, you can write custom import hooks.

> 19. Coroutines and threads (non-preemptive)

Threads, yes.  For corutines, check out greenlets or Stackless Python.


> 20. Date persistence and serialization

This can mean any one of a dozen things.


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

Yes, though it isn't done very often.

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

It is not hidden, in fact, you get to name it...

class foo:
    def goo(I_AM_SELF, arg):
        pass

> 23. Prototype-based OOP or the ability to extend a class without 
> physically modifying it

We have unlimited subclassing.  If you want prototypes, check out
Prothon.

> 24. Manual garbage management

You can disable it, or force collection.

gc.disable()
gc.collect()

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

Check IronPython and PythonNET

> 26. How easily can other languages access it and vice versa?

C and C++ are easy.  There is a TCL interface.

Anything with C or C++ interfaces are arguably trivial to talk to.

> 27. The option of mixing in static typing

Python has no static typing.

> 28. Automatic type coercion

Certain kinds of things can do what is known as "type coercion" in other
languages.  What kinds of coercion did you want to do.

> 29. Is Python designed in such a way that I may merely "plugin" a 
> C/C++/Java/D module which will then allow for mixing their syntax and 
> perhaps even access to their facilities within Python?

You can call C/C++ functions in Python (a large portion of the standard
library is implemented in C).

Jython allows nearly transparent use of Python and Java.

You don't get other language syntaxes, generally.

> 30. Messaging syntax such as : [myColor setRed:0.0 green:0.5 blue:1.0] 
> or [dog bring:paper to:me] and [[myAunt phone] setTo:[myUncle phone]] 
> <--- Nested messages and [dog perform:sel_get_uid("bark")] which is the 
> same as [dog bark]

We don't do messaging syntax.  Combining messaging and object-oriented
syntax would make for an ugly language.

> 31. Concepts of Protocols (whereby one may organize related methods into 
> groups and check whether a particular object implements the methods 
> within this protocol), or Interfaces similar to those in Java whereby 
> classes or objects which implement the interface (sign the contract) 
> must implement the methods and attributes as specified in the interface, 
> and/or programming by contract such as in Eiffel (see: 
> http://www.devhood.com/tutorials/tutorial_details.aspx?tutorial_id=595)

No.  Test-driven development is the norm in Python.

> 32. Support for unplanned reuse of classes such as in TOM 
> (http://www.gerbil.org/tom/)

Subclassing.

> 33. Function/Method overloading

Yes.

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

Yes, if you are willing to work for it.

> 35. May I modify the garbage collector?

If you really want, though it already works fine.

> 36. May I implement control structures as object messages?

If you want.

> 37. Dynamic dispatch

This could mean a few different things.

> 38. Reflection and/or templates

Think subclasses.

> 39. Unicode

Yes.

> 40. Ability to call external APIs and DLLs with relative ease

Check pywin32.

> 41. How easy is it to port my Python code to C/C++/C# or Java?

Depends on how experienced you are with those languages.

> 42. The ability to assign a method(s) to a collection/group of objects 
> [with a particular signature i.e.]

No.

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

t = time.asctime()
print "Hello World %(t)s"%{'t':t}
print "Hello World %(t)s"%locals()

> 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

No.

> 45. Are all things objects in Python?  Do all objects have built-in 
> iterators and the like?  i.e. can I do this:
>     3.times { print "Ho! " }
>     puts "Merry Christmas"

Not everything has an iterator, though many does.


Next time, read the damn documentation.

 - Josiah




More information about the Python-list mailing list