Talking about old and new Python classes

Even if you use it for more than 5 years, it is a candy ball for Python beginners

So, there is such a description in a book

class SomeClass(object):
  ..

I wondered what this "object" is, so make a note.

Python class inherits object(Stack Overflow)

In Python2 series, there are two types of classes, and if you are not newer, you will get a lot of information about types. Since there is only a new person in Python3, it seems to be one of the dying knowledge.

Well I will try it. There is also love that you can see from the results

class_exp.py


class OldClass:
  def a(self):
    print("I'm OldClass")

class OldChild(OldClass):
  def a(self):
    super(OldChild, self).a()

class NewClass(object):
  def a(self):
    print("I'm NewClass")

class NewChild(NewClass):
  def a(self):
    super(NewChild, self).a()

x = OldChild()
y = NewChild()
print(type(x))
print(type(y))
print('--')
try:
  # Will fail with Python 2.x
  x.a() 
except TypeError as e:
  import traceback
  traceback.print_exc()
print('--')
y.a()
> python2.7 misc/class_exp.py
<type 'instance'>
<class '__main__.NewChild'>
--
Traceback (most recent call last):
  File "misc/class_exp.py", line 27, in <module>
    x.a()
  File "misc/class_exp.py", line 10, in a
    super(OldChild, self).a()
TypeError: must be type, not classobj
--
I'm NewClass

> python3.2 misc/class_exp.py
<class '__main__.OldChild'>
<class '__main__.NewChild'>
--
I'm OldClass
--
I'm NewClass

In Python2, the objects in the older class don't look as the type expected. There seems to be one point in the point that the result of type (obj) is `` <type'instance'>''. That's the relationship, I can't see the parent class correctly with super ().

Then read the old documentation.

(2 PEPs 252 and 253: Type and Class Changes)[http://docs.python.org/release/2.2.3/whatsnew/sect-rellinks.html]

By the way, the book at the beginning is a book that mainly focuses on Python3, so I'm not sure why I explicitly specified objects that are not needed in Python3.

Recommended Posts

Talking about old and new Python classes
[Python] About Executor and Future classes
Talking about Python class attributes and metaclasses
About Python, len () and randint ()
About Python datetime and timezone
About Python and regular expressions
About Python and os operations
Python # About reference and copy
About Python sort () and reverse ()
About installing Pwntools and Python2 series
Python ABC-Abstract Classes and Duck Typing
Python: A Note About Classes 1 "Abstract"
About python dict and sorted functions
About dtypes in Python and Cython
About Python pickle (cPickle) and marshal
About Python, from and import, as
Python classes and instances, instance methods
About _ and __
Bind methods to Python classes and instances
A story about Python pop and append
About python slices
About python comprehension
[Introduction to Python3 Day 12] Chapter 6 Objects and Classes (6.3-6.15)
About Python tqdm.
About python yield
About python, class
How python classes and magic methods are working.
Think about depth-priority and width-priority searches in Python
About the difference between "==" and "is" in python
About python inheritance
[Introduction to Python3 Day 11] Chapter 6 Objects and Classes (6.1-6.2)
About python, range ()
[Python for Hikari] Chapter 09-02 Classes (Creating and instantiating classes)
About python decorators
About python reference
About Python decorators
A story about modifying Python and adding functions
[Python] Learn about asynchronous programming and event loops
[Python] About multi-process
About the * (asterisk) argument of python (and itertools.starmap)
About shallow and deep copies of Python / Ruby
Getting Started with python3 # 2 Learn about types and variables
About creating and modifying custom themes for Python IDLE
[Python] Chapter 01-02 About Python (Execution and installation of development environment)
[python] Compress and decompress
About Class and Instance
About function arguments (python)
Batch design and python
Python iterators and generators
Python packages and modules
Vue-Cli and Python integration
[Python] Memo about functions
Python classes are slow
Ruby, Python and map
Summary about Python3 + OpenCV3
About cumprod and cummax
About Python, for ~ (range)
python input and output
[Python] Memo about errors
About Python development environment
Python: About function arguments