overexchange
overexchange

Reputation: 1

Adding a new attribute to Python object?

Being from OOPS background, It looks strange to see below code from link

def f():
    f.beencalled = True
    return 0

My question:

1) From the above code, Is f a reference variable pointing to an object f of class 'function'?

2) We add a new attribute beencalled to an object f, so now 'function' class does not have this attribute beencalled defined and we say that object f is an object of class 'function'? Does it make sense?

Upvotes: 0

Views: 2151

Answers (3)

ApproachingDarknessFish
ApproachingDarknessFish

Reputation: 14323

1) Yes:

>>> def f():
        print(type(f))

>>> f()
>>> <class 'function'>

2) The function class does not have a new attribute, but the object f does. Adding or removing attributes to/from an object does not affect which attributes other objects of that class will have:

>>> class A: pass
>>> a = A()
>>> a.var = 7
>>> b = A()
>>> b.var
Traceback (most recent call last):
File "<pyshell#19>", line 1, in <module>
    b.newvar
AttributeError: 'A' object has no attribute 'var'

Classes are much more flexible in python than in Java or C++. Objects can have attributes not defined in their class, or even lack attributes that were defined in their class! Look at this:

>>> class A:
def __init__(self, a):
    self.var = a


>>> obj = A(7)
>>> del obj.var #deletes the var attribute from obj, does not change the A class
>>> obj.var
Traceback (most recent call last):
  File "<pyshell#28>", line 1, in <module>
    obj.var
AttributeError: 'A' object has no attribute 'var'

>>> obj2 = A(6)
>>> obj2.var #obj2 is a new object, so the fact we deleted var from obj doesn't affect it
6 

EDIT: after a bit of searching I found an explanation for why this behavior was chosen (source):

To implement user-defined objects, I settled on the simplest possible design; a scheme where objects were represented by a new kind of built-in object that stored a class reference pointing to a "class object" shared by all instances of the same class, and a dictionary, dubbed the "instance dictionary", that contained the instance variables.

In this implementation, the instance dictionary would contain the instance variables of each individual object whereas the class object would contain stuff shared between all instances of the same class--in particular, methods. In implementing class objects, I again chose the simplest possible design; the set of methods of a class were stored in a dictionary whose keys are the method names. This, I dubbed the class dictionary. To support inheritance, class objects would additionally store a reference to the class objects corresponding to the base classes. At the time, I was fairly naïve about classes, but I knew about multiple inheritance, which had recently been added to C++. I decided that as long as I was going to support inheritance, I might as well support a simple-minded version of multiple inheritance. Thus, every class object could have one or more base classes.

In this implementation, the underlying mechanics of working with objects are actually very simple. Whenever changes are made to instance or class variables, those changes are simply reflected in the underlying dictionary object. For example, setting an instance variable on an instance updates its local instance dictionary. Likewise, when looking up the value of a instance variable of an object, one merely checks its instance dictionary for the existence of that variable. If the variable is not found there, things become a little more interesting. In that case, lookups are performed in the class dictionary and then in the class dictionaries of each of the base classes.

Upvotes: 5

Ashwini Chaudhary
Ashwini Chaudhary

Reputation: 251146

f() in just an instance of types.FunctionType, and instances can have their own attributes.

Adding an attribute to an instance won't affect its class unless you've overridden the __setattr__ method of that class and doing something evil there.

>>> import types
>>> def func(): pass
>>> isinstance(func, types.FunctionType)
True

Upvotes: 1

Adam Smith
Adam Smith

Reputation: 54243

On a slightly different note, you can change this behavior for custom classes.

class FooBar(object):
    __slots__ = ["foo","bar","baz"]
    # if you don't define __slots__, you can add attr to the object as needed
    # if you do, the object can only contain those attributes.
    def __init__(self,foo=None,bar=None,baz=None):
        self.foo = foo
        self.bar = bar
        self.baz = baz
    def __str__(self):
        return "I'm a FooBar with id {0} with foo: {1.foo}, bar: {1.bar}, baz: {1.baz}".format(id(self),self)

>>> a = FooBar("a","B","CCC")
>>> print(a)
I'm a FooBar with id 47260256 with foo: a, bar: B, baz: CCC
>>> a.spam = "eggs"
Traceback (most recent call last):
  File "<pyshell#13>", line 1, in <module>
    a.spam = "eggs"
AttributeError: 'FooBar' object has no attribute 'spam'

Alternately, without defining __slots__:

class BooFar(object):
    def __str__(self):
        return "I'm a BooFar with the following attributes:\n{}".format(self.__dict__)

>>> b = BooFar()
>>> print(b)
I'm a BooFar with the following attributes:
{}
>>> b.spam = "eggs"
>>> print(b)
I'm a BooFar with the following attributes:
{'spam': 'eggs'}

Upvotes: 2

Related Questions