1

'Every thing in python is an object'

So, should all objects have to have attributes and methods ?

I read below statemts in tutorial site, could you give example of pre-defined object in python that has neither attributes nor methods ?

Some objects have neither attributes nor methods

m0nhawk
  • 22,980
  • 9
  • 45
  • 73
anati
  • 264
  • 2
  • 13
  • Presumably you mean http://www.diveintopython.net/getting_to_know_python/everything_is_an_object.html, so have you read chapter three for the promised follow-up? – jonrsharpe Apr 17 '17 at 07:38
  • Possible duplicate of [Is everything an object in python like ruby?](http://stackoverflow.com/questions/865911/is-everything-an-object-in-python-like-ruby) – GIZ Apr 17 '17 at 07:59

2 Answers2

1

Everything in Python is indeed an object, this is true. Even classes themselves are considered to be objects and they're indeed the product of the builtin class typewhich is not surprisingly an object too. But objects will almost certainly inherit attributes including methods or data in most circumstances.

So, should all objects have to have attributes and methods ?

Not necessarily all objects have their own attributes. For instance, an object can inherit the attribute from its class and its class's superclasses, but that attribute or method doesn't necessarily live within the instance's namespace dictionary. In fact, instances' namespaces could be empty like the following:

class Foo: 
    pass 

a = A()
print(a.__dict__) 

a here doesn't have any attributes aside from those inherited from its class so if you check its namespace through the builtin attribute __dict__ you'll find the namespace to be an empty dictionary. But you might wonder isn't a.__dict__ an attribute of a? Make a distinction between class-level attributes--attributes inherited from the class or its superclasses and instance-attributes--attributes that belong to the instance and usually live in its namespace __dict__.

Could you give example of pre-defined object in python that has neither attributes nor methods ?

If you meant by predefined object, a builtin object, I couldn't imagine such scenario. Again, even if there are no attributes at the object itself, there would be attributes inherited from its class or the class's superclasses if there's any superclass in most cases. Probably and I'm guessing here, the tutorial is asking you to create class that assigns no attributes to its objects, just like the code I included above.

And this already answers your question better: Is everything an object in python like ruby?

GIZ
  • 4,409
  • 1
  • 24
  • 43
1

There's a hackish way to emulate a Python object with no attributes.

class NoAttr(object):
    def __getattribute__(self, attr):
        raise AttributeError("no attribute: %s" % attr)

    def __setattr__(self, attr, value):
        raise AttributeError("can't set attribute: %s" % attr)

    def __delattr__(self, attr):
        raise AttributeError("no attribute: %s" % attr)

a = NoAttr()

This instance a, for all intents and purposes, in pure Python, behaves like an object with no attributes (you can try hasattr on it).

There may be a low-level way to do this in a C extension by implementing a type in C that pathologically stops Python's object implementation from working. Anyway the margin here is too small for writing one.

A pre-defined object with no attributes would defeat the purpose of pre-defining it.

Cong Ma
  • 10,692
  • 3
  • 31
  • 47