0

I am learning Python and I heard all functions are objects. And classes are objects too, if I am not mistaken. Then methods within a class must be objects.

Then I wonder, within __init__ method, which is an object, are the attributes objects, too? Objects within an object?

There is a phrase 'Everything is an object'. And my impression is that anything that holds a memory space is an object. What I don't quite grasp is to what extent the phrase 'everything is an object' applies. I wonder if it applies even to variables within __init__ method.

Sean
  • 57
  • 1
  • 5
  • 1
    Yes, the parameters you pass and the attributes you assign are also (references to) objects. – jonrsharpe Jun 24 '20 at 22:42
  • 1
    The answer to pretty much all of your questions is "yes" – Brad Solomon Jun 24 '20 at 22:42
  • Every thing in the physical world is matter. Same concept. – JacobIRR Jun 24 '20 at 22:45
  • 2
    Why would the `__init__` method be any different from any other method? – Barmar Jun 24 '20 at 22:47
  • Then, to be more precise, when you say objects, they are essentially "references to the objects"? – Sean Jun 24 '20 at 23:06
  • 1
    @Sean: Names are bound to objects, and can be rebound; "references" is problematic for people from a C++ background (they're not C++ references in any meaningful sense). They're largely similar to C++ `shared_ptr`s, where the *pointers* are passed around by value (and automatically reference counted). Assignment assigns the pointer, all other uses call methods on the object being pointed to, implicitly (e.g. `+` invokes `__add__`) or explicitly. – ShadowRanger Jun 24 '20 at 23:13
  • What is your 1 specific researched non-duplicate question? PS Who says 'everything is an object' means anything? What justification was given where you saw that in an authoritative reference? What did the author of whereever you heard is claim it meant? It's clearly just a mnemonic for something much larger. Find out what 'object' means & what all those other terms mean & ask a question where you are stuck reading an authoritative presentation or in justifying it yourself to us. Right now you're just asking us to rewrite a textbook & to guess at what somebody meant by something informal. – philipxy Jul 01 '20 at 04:55

1 Answers1

-4

Yes, it's quite similar to Java. Every Python variable hides a pointer to the right location of memory which contains the actual data. There are only four primitive types in Python: integers, floats, booleans and strings; anything else is an ADT. The keyword self is the same of this in Java or C++, in other words the self-reference. Using self, you can invoke another method inside the class or access to an attribute. Syntax like self.var = 1 can also creates a new attribute (if it doesn't exist yet) with name "var", type "int" and value 1.

A. May
  • 69
  • 4
  • 2
    There aren't *any* primitive types in Python, that's what "everything is an object" really means. – jonrsharpe Jun 24 '20 at 22:52
  • @jonrsharpe Python has four primitive types: integers, floats, booleans and strings. – A. May Jun 24 '20 at 22:56
  • 2
    They're still objects, not primitive in any meaningful sense; they have methods, etc. See e.g. https://stackoverflow.com/a/6392976/3001761 – jonrsharpe Jun 24 '20 at 22:58
  • I use this exemple. "a = 2; print(a); foo(a); print(a); def foo(a): a=a+1;". This code prints "2 2". Even if technically ints are object, their behaviour is identical to a classical primitive type (Java ints for instance). – A. May Jun 24 '20 at 23:05
  • 1
    @A.May: You're likely confused because in many ways they *act* like primitives; they're immutable, so even when you do `x = 5`, `y = x`, `y += 1`, `x` remains unchanged (similar actions with a `list` would mutate `x`, because `x` and `y` would remain aliases; `+=` operates in-place only on mutable types). But they're still full-fledged objects, they have the common Python object header, they're heap-allocated, they have methods, etc. You could compare them to Java's *boxed* primitives (e.g. `Integer`, `Boolean`), which is basically what they are. – ShadowRanger Jun 24 '20 at 23:06
  • 1
    That example has an error, foo is used before it's defined. Fixing that it still doesn't illustrate what you claim; you'd get the same behaviour with `a = [2]` and `a = a + [1]`, for example, and lists aren't a type you consider primitive. – jonrsharpe Jun 24 '20 at 23:11
  • 1
    Also Python is strongly *but dynamically* typed; `self.var = 1` doesn't create an attribute with type int, it creates an attribute whose ^current value* has the type int. – jonrsharpe Jun 24 '20 at 23:17
  • 1
    @A.May that works exactly the same with any object, your exaple demonstrates nothing. **There are no primitive types in Python**. – juanpa.arrivillaga Jun 24 '20 at 23:17
  • Primitive types are intended like "passed by value" variable because of their immutability. It was just an easier explanation. A non-primitive type would fail in the previous exemple and it would modify 'a'. – A. May Jun 24 '20 at 23:23
  • 1
    I've just shown you an example with a list that disproves that. Python doesn't pass e.g. ints in a different way to e.g. lists. – jonrsharpe Jun 24 '20 at 23:26
  • 2
    @A.May no it wouldn't. The evaluation strategy is **exactly the same** for **all types** in Python. Again, try `def foo(x): x = x + [1]; y = []; foo(y); print(y)` the difference is that *int objects lack any mutator methods*, i.e., they are immutable, but if you really want to, you can use `ctypes` to modify the object (relying on implementation details) and you will see that `int` objects are really objects, and they are passed to functions **exactly** like any other object Python, unlike Java, is purely object oriented in this regard, there *are no non-object types* – juanpa.arrivillaga Jun 24 '20 at 23:26
  • I've just learned a new thing. I was absolutely sure about the analogy with Java. Sorry guys. – A. May Jun 24 '20 at 23:34