Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why 'object' class doesn't have user set attributes

Tags:

python

Every class in python inherits from the class 'object.' I am wondering about the internal mechanism of the 'object' class implementation. Why cannot the 'object' class be assigned any user-attributes? I am convinced that it is related to memory-management but what if the user thinks of implementing the memory-management himself, why can't he override 'object' class in python? This is based on my interest and want to know about which might not have any programmatic application but would be nice to know the internal feature of the language itself.

like image 774
Jack_of_All_Trades Avatar asked Apr 11 '12 20:04

Jack_of_All_Trades


People also ask

What is object class attribute?

A class attribute is a variable that belongs to a certain class, and not a particular object. Every instance of this class shares the same variable. These attributes are usually defined outside the __init__ constructor. An instance/object attribute is a variable that belongs to one (and only one) object.

How do I add an attribute to a class?

Adding attributes to a Python class is very straight forward, you just use the '. ' operator after an instance of the class with whatever arbitrary name you want the attribute to be called, followed by its value.

What is the difference between class attributes and instance attributes?

Differences Between Class and Instance Attributes The difference is that class attributes are shared by all instances. When you change the value of a class attribute, it will affect all instances that share the same exact value. The attribute of an instance on the other hand is unique to that instance.


2 Answers

Types defined in C can never have user-assigned attributes. If you want to replace object globally then you'll need to iterate over every single class in existence, then replace __builtin__.object to catch all future class creation. And even that isn't reliable since the old object could be bound somewhere else.

like image 123
Ignacio Vazquez-Abrams Avatar answered Sep 20 '22 14:09

Ignacio Vazquez-Abrams


This has been discussed on StackOverflow, but I'm having trouble finding the discussion to link it.

The reason I wondered is because I wanted to simplify this example:

class Box(object):
    pass

box = Box()
box.a = "some value"
box.b = 42

Here I'm using box as a sort of dictionary, one where the keys can only be identifiers. I do this because it's more convenient to write box.a than box["a"].

I wanted to do this:

box = object()
box.a = "some value"  # doesn't work

The reason is that object is the root of all objects in Python. Any attributes that object has must be in any derived type. If you want to make a large list containing a large number of objects, you want them to be as small as possible so you don't run out of memory. So object itself is minimal.

EDIT: I found the link I was trying to remember:

struct objects in python

like image 41
steveha Avatar answered Sep 19 '22 14:09

steveha