简体   繁体   中英

In what instance would I require parameters in python classes?

Let's assume I have the following code:

x = 1
y = 1
class foo(x):
    def __init__(y):
        print x
        print y
test = foo(x)

This code obviously will not work. But in what instance would you pass a variable through the class itself, in this case x?

Have a look at the docs :

class foo(x): means that class foo inherits from parent class x .

When creating an instance of the class, the __init__ method is called with parameters (of which the first one should always be self ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM