Python method calls the class attribute Detailed - python

Source: Hi learning network sensitive and eager Forum www.piaodoo.com welcome to learn from each other

When calling the test class it is often used. Calls use simple look like it.

Look at an example:

Directory Structure:

Hi learning network

We are now going to call class_learn.py in the class in this file do_class.py

Code (do_class.py):

#!/usr/bin/env python3
#coding=utf-8
'''@Author:Jock'''

from all_python_learn.class_and_funcation.class_learn import *

b = Learn(1,2)

b.get()
print(Learn.a)

By introducing this module from class_learn in the class, then instantiates an instance of a class by b object, then the object b calls the get function class. Here I added a print (Learn.a) just to say directly through the class name connected to the variable name can call the variable class.

operation result:

Hi learning network

If we do not instantiate what the outcome will directly call it? come and see

Modify do_class.py as follows:

Hi learning network

If so it would be an error to write, because there is no instantiated.

Error:

Hi learning network

The above example is based band pass parameters may have classes with no parameters, and then calls.

The class_learn.py into the following:

Hi learning network

In do_class.py you can write:

Hi learning network

operation result:

Hi learning network

By the above example the basic know how to call the functions within classes, class of variables (this is very important, often write access to files across the class of property use case !!)

But I guess you compare to self doubts after reading the above code, in fact, have talked about the definition of the beginning of the class, this must be written, it is an instance of the class.

Examples in the initialization function in the

self.x = x # where x and y variables are examples
self.y = y

This is the definition of the two variables x and y, and assigns and examples self.x self.y, all functions in the back to use if the two variables are required to call self.x by writing, and argument list must function with self, and pass inside the examples. Otherwise the call will throw an error.

The original address is: http: //www.piaodoo.com/thread-14087-1-50.html Crazy Love www.eplbx.com 131 www.buzc.org stockings control www.txdah.com to learning can be enjoyable help to learn better! Despite the very good study may help to better learning enjoyable! very good!

Guess you like

Origin www.cnblogs.com/txdah/p/12128443.html