1

I was experimenting with Python, and tried out an example and it worked.

def foo():
 print 'hello world'
foo.a = True

So it looks to me foo is an object though it looks like a function. What is the technical term for it, and what are the use-cases of it?

Martijn Pieters
1.1m326 gold badges4.2k silver badges3.4k bronze badges
asked Mar 9, 2014 at 12:03
3
  • 2
    Python functions are objects too. Commented Mar 9, 2014 at 12:05
  • 1
    plz google "function attributes" ;) Commented Mar 9, 2014 at 12:06
  • @zhangxaochen Ya, it has a technical term, I was looking for same, thnx..:-) Commented Mar 9, 2014 at 12:09

3 Answers 3

1

Everything is an object. At least almost everything. There are very few things in Python in which you can not create attributes. Probably only some of the things that are implemented in C and exposed as Python objects, as they may be not supporting the necessary interface. For example, you can not create attributes inside objects of type int, for it does not have a dictionary of attributes (mostly due to performance reasons, I suppose). But it still is an object, though with limited functionality.

answered Mar 9, 2014 at 12:06
Sign up to request clarification or add additional context in comments.

8 Comments

Name one thing that's not!
@holdenweb: keywords such as in, with, and return are not objects.
@unutbu are they things?
Gentlemen, names are references to objects. When you try to create an attribute, you create it in an object the name refers. Keywords do not exist in a compiled Python program, and they are used only by lexer/parser.
@zhangxaochen: many operators have equivalent functions in the operators module. The ast module defines syntax tree objects that model syntax constructs. So they could be, in certain contexts.
|
0

Yes, functions in Python are objects as everything else:

Python 3.3.2+ (default, Oct 9 2013, 14:50:09) 
>>> def a(): pass
... 
>>> type(a)
<class 'function'>
>>> import types
>>> types.FunctionType
<class 'function'>
>>> type(a) is types.FunctionType
True
>>> 
answered Mar 9, 2014 at 12:26

Comments

0

In Python 3, the inheritance hierarchy is firmly rooted at object, but in Python 2 things are less clear-cut. While it's true for both version that "everything is an object" the real question is "what type of object"?

So here's a program that it's instructive to run under both versions (I hope).

class OldStyle:
 pass
class NewStyle(object):
 pass
old_obj = OldStyle()
new_obj = NewStyle()
print(OldStyle, NewStyle)
print(old_obj, new_obj)

To make it easier to see what's going on I simply pasted the program into different interepreters, the output being shown below.

airhead:project sholden$ python2
Python 2.7.6 (default, Nov 19 2013, 03:12:29)
[GCC 4.2.1 Compatible Apple LLVM 4.2 (clang-425.0.28)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> class OldStyle:
... pass
...
>>> class NewStyle(object):
... pass
...
>>> old_obj = OldStyle()
>>> new_obj = NewStyle()
>>>
>>> print(OldStyle, type(OldStyle))
(<class __main__.OldStyle at 0x105345808>, <type 'classobj'>)
>>> print(NewStyle, type(NewStyle))
(<class '__main__.NewStyle'>, <type 'type'>)
>>> print(old_obj, type(old_obj))
(<__main__.OldStyle instance at 0x10535ff38>, <type 'instance'>)
>>> print(new_obj, type(new_obj))
(<__main__.NewStyle object at 0x105360bd0>, <class '__main__.NewStyle'>)
>>>

Versus

airhead:project sholden$ python3
Python 3.3.2 (default, Nov 19 2013, 03:15:33)
[GCC 4.2.1 Compatible Apple LLVM 4.2 (clang-425.0.28)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> class OldStyle:
... pass
...
>>> class NewStyle(object):
... pass
...
>>> old_obj = OldStyle()
>>> new_obj = NewStyle()
>>>
>>> print(OldStyle, type(OldStyle))
<class '__main__.OldStyle'> <class 'type'>
>>> print(NewStyle, type(NewStyle))
<class '__main__.NewStyle'> <class 'type'>
>>> print(old_obj, type(old_obj))
<__main__.OldStyle object at 0x106884c90> <class '__main__.OldStyle'>
>>> print(new_obj, type(new_obj))
<__main__.NewStyle object at 0x106884c50> <class '__main__.NewStyle'>
>>>

You will see that there are actually relatively few significant differences between the two outputs, which is to Guido van Rossum's credit as a language designer, because a major departure took place around the time of the 2.2 release.

Focusing on the specifics, in Python 2.x a class that does not inherit from object is of type classobj, and its instances will be of type(instance). In Python 3.x, however, the class is of type type, and the instances are of type __main__.NewStyle.

In both versions I believe it is true that there is no object x for which the expression isinstance(object, x) evaluates to False.

answered Mar 9, 2014 at 18:38

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.