Stuck in Python official tutorial docs
Question:
A piece of Python code that expects a particular abstract data type
can often be passed a class that emulates the methods of that data
type instead. For instance, if you have a function that formats some
data from a file object, you can define a class with methods read()
and readline()
that get the data from a string buffer instead, and
pass it as an argument.
Instance method objects have attributes, too: m.__self__
is the
instance object with the method m()
, and m.__func__
is the
function object corresponding to the method.
I am stuck at Python tutorial doc, I can’t understand above docs. Can anyone explain it in a plain way?
with demo will be great. Some concept in python is very unfamiliar to me, I can’t get the meaning of author.
Answers:
A piece of Python code that expects a particular abstract data type can often be passed a class that emulates the methods of that data type instead. For instance, if you have a function that formats some data from a file object, you can define a class with methods read() and readline() that get the data from a string buffer instead, and pass it as an argument.
Consider something like this.
def get_first_line_twice(file):
line = file.readline()
return line + line
This is "A piece of Python code that expects a particular abstract data type". "a particular abstract data type" is, in this case, a file.
"a class that emulates the methods of that data type instead" means a class that also has a readline()
method that behaves similarly. E.g.
class Foo:
def readline(self):
return "foo"
We can pass an instance of Foo
instead of a file to our function, and it will work without errors. That’s what the paragraph means.
Instance method objects have attributes, too: m.__self__ is the instance object with the method m(), and m.__func__ is the function object corresponding to the method.
Consider the Foo
class from previous example.
f = Foo()
f.readline #the method
f #"the instance object with the method"
f.readline.__self__ is f #True
f.readline.__func__ #"the function object corresponding to the method"
The last line essentially returns readline
decoupled from f
. If we want to call it, we will actually need to pass an object as the self
parameter, i.e. f.readline.__func__(f)
. Or f.readline.__func__(some_other_object)
. This can be useful for advanced functional programming, or maybe reflection. As a beginner, you can ignore these attributes for now.
A piece of Python code that expects a particular abstract data type
can often be passed a class that emulates the methods of that data
type instead. For instance, if you have a function that formats some
data from a file object, you can define a class with methodsread()
andreadline()
that get the data from a string buffer instead, and
pass it as an argument.Instance method objects have attributes, too:
m.__self__
is the
instance object with the methodm()
, andm.__func__
is the
function object corresponding to the method.
I am stuck at Python tutorial doc, I can’t understand above docs. Can anyone explain it in a plain way?
with demo will be great. Some concept in python is very unfamiliar to me, I can’t get the meaning of author.
A piece of Python code that expects a particular abstract data type can often be passed a class that emulates the methods of that data type instead. For instance, if you have a function that formats some data from a file object, you can define a class with methods read() and readline() that get the data from a string buffer instead, and pass it as an argument.
Consider something like this.
def get_first_line_twice(file):
line = file.readline()
return line + line
This is "A piece of Python code that expects a particular abstract data type". "a particular abstract data type" is, in this case, a file.
"a class that emulates the methods of that data type instead" means a class that also has a readline()
method that behaves similarly. E.g.
class Foo:
def readline(self):
return "foo"
We can pass an instance of Foo
instead of a file to our function, and it will work without errors. That’s what the paragraph means.
Instance method objects have attributes, too: m.__self__ is the instance object with the method m(), and m.__func__ is the function object corresponding to the method.
Consider the Foo
class from previous example.
f = Foo()
f.readline #the method
f #"the instance object with the method"
f.readline.__self__ is f #True
f.readline.__func__ #"the function object corresponding to the method"
The last line essentially returns readline
decoupled from f
. If we want to call it, we will actually need to pass an object as the self
parameter, i.e. f.readline.__func__(f)
. Or f.readline.__func__(some_other_object)
. This can be useful for advanced functional programming, or maybe reflection. As a beginner, you can ignore these attributes for now.