【问题标题】:Javascript `this` vs Python `self` ConstructorsJavascript `this` vs Python `self` 构造函数
【发布时间】:2018-01-16 10:17:01
【问题描述】:

Javascript 构造器 + 创建对象示例

//Constructor
function Course(title,instructor,level,published,views){
    this.title = title;
    this.instructor = instructor;
    this.level = level;
    this.published = published;
    this.views = views;
    this.updateViews = function() {
        return ++this.views;
    }
}

//Create Objects
var a = new Course("A title", "A instructor", 1, true, 0);
var b = new Course("B title", "B instructor", 1, true, 123456);

//Log out objects properties and methods
console.log(a.title);  // "A Title"
console.log(b.updateViews()); // "123457"

什么是 python 等价物? (构造函数/或类+创建对象实例+注销属性和方法?)

python中的self和Javascript中的this有区别吗?

【问题讨论】:

  • 差不多,没区别。尽管在 python 中,self 是约定俗成的,你可以随意称呼它。此外,您必须将其作为构造函数的第一个参数包含在内,而 JS “只是本能地知道”this 是什么
  • 您的问题标题已转换术语
  • 哎呀没意识到会交换

标签: javascript python object constructor


【解决方案1】:

这是一个python翻译:

class Course:

    def __init__(self,title,instructor,level,published,views)
        self.title = title
        self.instructor = instructor
        self.level = level
        self.published = published
        self.views = views

    def update_views(self):
        return self.views += 1

您必须声明一个类,然后按如下方式初始化该类的一个实例

course = Course("title","instructor","level","published",0)

一些显着的区别是self 不是隐式可用的,但实际上是类的所有方法的必需参数。但是,您应该咨询 the documentation 以了解更多信息。

编辑

截至python3.7,我有义务证明新引入的dataclasses 是编写此类类的最pythonic 方式,并且可能对您未来的python 开发人员有所帮助。

from dataclasses import dataclass

@dataclass
class Course:

     title: str 
     instructor: str 
     level: str 
     published: bool
     views: int 

     def update_views(self) -> int:
         return self.views += 1 

【讨论】:

  • 所以在python中你必须在一个类中单独定义属性/方法(使用def)?
  • @Kagerjay 嗯?不,基本上 Python 和 Javascript 有不同的 OOP 模型。 Javascript 使用基于原型的继承,而 Python 使用基于类的继承。我相信类是在 ECM6 中引入的。请注意,您可以在构造函数中为 Python 中的实例动态添加函数属性,但它们不会被继承。
  • 好吧,我将不得不做更多的阅读感谢您提供的信息:)
  • @modesitt 你能否澄清我在下面发布的答案,我无法弄清楚如何在此处访问方法/打印结果
  • @Kagerjay 我肯定会的。给我一分钟。
【解决方案2】:

python解决方案出现了一些错误,现在修复了

#Constructors
class Course:

    def __init__(self,title,instructor,level,published,views):

        self.propTitle = title
        self.propInstructor = instructor
        self.propLevel = level
        self.propPublished = published
        self.propViews = views

    def update_views(self):
        self.propViews += 1
        return self.propViews

# Create objects
courseA = Course("A title", "A instructor", 1, True, 0)
courseB = Course("B title", "B instructor", 1, True, 123456)

# Print object property and use object method
print(courseA.propTitle)
print(courseB.update_views())

结果打印出来

标题

123457

虽然使用 print(courseB.update_views) 会输出这个,<bound method Course.update_views of <__main__.Course object at 0x7f9f79978908>> ,使用 print(courseB.update_views())

【讨论】:

  • @Igor 是对的。您必须调用该方法。它不是计算机财产。
  • 好的,谢谢我现在修好了,我把所有东西都命名为prop前缀,这样我就清楚哪些值是参数,哪些不是
猜你喜欢
  • 2011-05-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-01
相关资源
最近更新 更多