jim stockford wrote:
> from
> http://docs.python.org/lib/built-in-funcs.html
> some functions are always available--the built-in functions.
>
> (from elsewhere) everything in python is an object.
>
> --------------------
>
> hey, from abs() to zip() there's type() and super() and str()
> and setattr() and ... dir() and... they're the built-ins
>
> my question: what.abs()  what.zip() etc.?
>
> I think there must be a base object, a la java or Nextstep,
> that supports these functions. what is it?
>
> maybe it's not practical, but it's driving me nuts anyway.
> thanks in advance.
>
>
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>   
Hi Jim, in Python function are objects also.

 >>> def test_func():
...     return 1

 >>> abs.__class__.__name__
'builtin_function_or_method'
 >>> test_func.__class__.__name__
'function'
 >>> from types import FunctionType, BuiltinFunctionType
 >>> isinstance(abs, BuiltinFunctionType)
True
 >>> isinstance(test_func, FunctionType)
True

Each function is an object. When you call

foo()

It's actually shorthand for

foo.__call__()

Try the dir() command - it lists all attributes of an object. For instance:
 >>> dir(abs)
['__call__', '__class__', '__cmp__', '__delattr__', '__doc__', 
'__getattribute__', '__hash__', '__init__', '__module__', '__name__', 
'__new__', '__reduce__', '__reduce_ex__', '__repr__', '__self__', 
'__setattr__', '__str__']
 >>> dir(test_func)
['__call__', '__class__', '__delattr__', '__dict__', '__doc__', 
'__get__', '__getattribute__', '__hash__', '__init__', '__module__', 
'__name__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', 
'__setattr__', '__str__', 'func_closure', 'func_code', 'func_defaults', 
'func_dict', 'func_doc', 'func_globals', 'func_name']

This means that unlike Java you can pass functions as arguments to other 
functions/methods - it also means you don't need to wrap everything in a 
class unnecessarily - in Python you only use a class when you need a 
class, not because Sun decided that classes were good. ;-)

Regards,

Liam Clarke
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to