New to Python: Features

Josiah Carlson jcarlson at uci.edu
Tue Oct 5 02:11:42 EDT 2004


> This is a cool language, I love code chunks!!

They can be useful, but they are a great way to introduce security
holes.


> >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
> > 
> 
> How come Python always complains when I don't include the self parameter 
> in an object method?  Your code doesn't work on my interpreter.

I don't know what is wrong with your interpreter...

>>> class foo:
...    def goo(self, arg):
...        print self, arg
...    def bar(arg):
...        print arg
...    bar = staticmethod(bar)
...
>>> a = foo()
>>> a.goo('hello')
<__main__.foo instance at 0x00909968> hello
>>> a.bar('hello')
hello
>>> 



> > No, it kills tracebacks.
> 
> I know but that's a real bummer.  No way around that?

Modify the interpreter.  There was a thread a few months back on
python-dev on doing tail-call optimization, I think someone even had a
patch for it.

Python's recursion limit is around 1000 levels deep.  If you are doing
tail-calls more than 1000 levels deep, maybe you should be looping
instead of recursing, or even creating your own stack.

> > No.
> > 
> 
> So is the print statement ironed into the language and not a function?

Yes.  It is a keyword, like a few others.

>>> import keyword
>>> keyword.kwlist
['and', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'e
xcept', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is',
'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'yiel


> yes, you can write custom import hooks.
> > 
> 
> I guess I'll hit the docs with how to do this.

I hear they aren't too hard.


> >>20. Date persistence and serialization
> > 
> > 
> > This can mean any one of a dozen things.
> >
> 
> I'd like to store function and variables and of course objects in a form 
> that can be easily read back into the program/script.

Functions, no (security risk).

Arbitrary Python objects, yes.  Check out the pickle module.


> > Python has no static typing.
> > 
> 
> Argg!

Don't be frustrated.  It is dynamically typed, but it is STRONGLY typed.

Anything that you would want to do with a static typed language, can be
done with Python.


> I'd like to be able to use a string as an integer without an explicit 
> coerion on my part for example.

Nope, but int("10") -> 10.


> >>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.
> > 
> 
> That is quite a shame, no modules for this either huh?

There is a unittest module for encouraging test driven development.

You can, of course, design by contract.  Nothing stops you from doing so.

I do what could be considered design by contract in much of my contract
work (no pun intended).  I also write extensive tests (roughly 1/3 of my
code is) to verify correctness.

When decorators via a syntax are introduced in Python 2.4, design by
contract becomes easier (you can set preconditions and postconditions,
or whatever you want).


Thinking about it, having never used Java, I imagine an "Interface"
would be something like...


class I_foo:
    def funct1(self):
        raise Exception, "Not Implemented"
    def funct2(self, arg):
        raise Exception, "Not Implemented"

class foo_implementation(I_foo):
    def funct1(self):
        pass
    def funct2(self, arg):
        pass


So, I suppose Python does, if implicitly.



> > Yes, if you are willing to work for it.
> > 
> 
> I expect the docs will tell me how to do this.

There was another thread on this.  Check the thread on this mailing list
with the subject of "Python Macros".


> Oh, I just like to play with garbage collectors.  It's a kind of fettish 
> of mine.

The Python garbage collector works well.  There isn't much need to
change it.


> Like virtual functions in C++

>>> class foo:
...     def goo(self):
...         print "foo.goo", self
...
>>> class hoo(foo):
...     def goo(self):
...         print "hoo.goo", self
...         foo.goo(self)
...
>>> hoo().goo()
hoo.goo <__main__.hoo instance at 0x00920508>
foo.goo <__main__.hoo instance at 0x00920508>


> What I actually meant was are there any facilities (built-in or modules) 
> which allow for automatic compilation to C/C++/C#/D.  For example, 
> particular Smalltalk distros compile to C as does ObjC.

Pyrex can compile a subset of Python to C.


> That's no good.  I wonder if I could implement this myself with minimum 
> pain.

If you can conceptualize the algorithm, you can likely do it in Python.


> That is messy, exactly why I wanted the kind of clear embedding I gave 
> an example for.  Oh well.

There is also...

print "Hello, World %s"%time.asctime()


> > No.
> > 
> 
> Really?  Hmmmm, does anyone know of a bridge between Python and Ruby?

I am not aware, but I have had no reason to learn Ruby.

There is always:

if score <= 40:
    print "Horrible!"
elif score <= 60:
    print "Poor"
elif score <= 80:
    print "You can do better!"
elif score <= 95:
    print "Now that's acceptable"
elif score <= 100:
    print "That the best you can do? j/k"
else:
    print "Didn't take the test?"

It isn't as fast as a real case statement, but if that is necessary...

class foo:
    def case_1(self, arg):
        pass
    def case_2(self, arg):
        pass
    def dispatch(self, arg1, arg2):
        getattr(self, 'case_%i'%arg1)(arg2)

 - Josiah




More information about the Python-list mailing list