Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between a 'Type' and an 'Object' in Python [duplicate]

Tags:

I came upon this reading the python documentation on the super keyword:

If the second argument is omitted, the super object returned is unbound. If the second argument is an object, isinstance(obj, type) must be true. If the second argument is a type, issubclass(type2, type) must be true (this is useful for classmethods).

Can someone please give me an example of a distinction between passing a Type as a second argument versus passing an Object?

Is the documentation talking about an instance of an object?

Thank you.

like image 575
claudio Avatar asked Mar 30 '13 06:03

claudio


People also ask

How do you duplicate an object in Python?

In Python, we use = operator to create a copy of an object. You may think that this creates a new object; it doesn't. It only creates a new variable that shares the reference of the original object. Let's take an example where we create a list named old_list and pass an object reference to new_list using = operator.

What is the difference between copy copy () and copy Deepcopy ()?

copy() create reference to original object. If you change copied object - you change the original object. . deepcopy() creates new object and does real copying of original object to new one. Changing new deepcopied object doesn't affect original object.

What is the difference between a shallow copy and a deep copy in Python?

A shallow copy constructs a new compound object and then (to the extent possible) inserts references into it to the objects found in the original. A deep copy constructs a new compound object and then, recursively, inserts copies into it of the objects found in the original.

Which object Cannot be copied in Python?

This module does not copy types like module, method, stack trace, stack frame, file, socket, window, array, or any similar types.


1 Answers

Python's super function does different things depending on what it's arguments are. Here's a demonstration of different ways of using it:

class Base(object):     def __init__(self, val):         self.val = val      @classmethod     def make_obj(cls, val):         return cls(val+1)  class Derived(Base):     def __init__(self, val):         # In this super call, the second argument "self" is an object.         # The result acts like an object of the Base class.         super(Derived, self).__init__(val+2)      @classmethod     def make_obj(cls, val):         # In this super call, the second argument "cls" is a type.         # The result acts like the Base class itself.         return super(Derived, cls).make_obj(val) 

Test output:

>>> b1 = Base(0) >>> b1.val 0 >>> b2 = Base.make_obj(0) >>> b2.val 1 >>> d1 = Derived(0) >>> d1.val 2 >>> d2 = Derived.make_obj(0) >>> d2.val 3 

The 3 result is the combination of the previous modifiers: 1 (from Base.make_obj) plus 2 (from Derived.__init__).

Note that it is possible to call super with just one argument to get an "unbound" super object, it is apparently not useful for much. There's not really any reason to do this unless you want to mess around with Python internals and you really know what you're doing.

In Python 3, you can also call super with no arguments (which is equivalent to the calls in the functions above, but more magical).

like image 96
Blckknght Avatar answered Sep 28 '22 02:09

Blckknght