class和def之间的区别 [英] Differences between `class` and `def`

查看:1567
本文介绍了class和def之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

class def 在python中的主要区别是什么? python中的类可以与django UI(按钮)交互吗?

What is the main difference between class and def in python? Can a class in python interact with django UI (buttons)?

推荐答案

class 用于定义一个类(可以从中实例化对象的模板)。

class is used to define a class (a template from which you can instantiate objects).

def 定义函数或方法。方法就像属于一个类的函数。

def is used to define a function or a method. A method is like a function that belongs to a class.

# function
def double(x):
    return x * 2

# class
class MyClass(object):
    # method
    def myMethod(self):
        print ("Hello, World")

myObject = MyClass()
myObject.myMethod()  # will print "Hello, World"

print(double(5))  # will print 10

对您所提问题的Django部分一无所知。也许应该是一个单独的问题?

No idea about the Django part of your question sorry. Perhaps it should be a separate question?

这篇关于class和def之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆