最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript `this` vs Python `self` Constructors - Stack Overflow

programmeradmin1浏览0评论

Javascript Constructor + create objects example

//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"

what is the python equivalent of this? (Constructor function / or class + create object instances + log out properties & methods?)

Is there a difference between self from python and this from Javascript?

Javascript Constructor + create objects example

//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"

what is the python equivalent of this? (Constructor function / or class + create object instances + log out properties & methods?)

Is there a difference between self from python and this from Javascript?

Share Improve this question edited Aug 8, 2017 at 18:30 Vincent Tang asked Aug 8, 2017 at 18:04 Vincent TangVincent Tang 4,1706 gold badges52 silver badges70 bronze badges 4
  • 4 Pretty much, no difference. Though in python, self is by convention, and you can call it anything you want. Also, you have to include it as the first parameter to your constructor, whereas JS "just instinctively knows" what this is – inspectorG4dget Commented Aug 8, 2017 at 18:07
  • Have you consulted the Python documentation on classes? – juanpa.arrivillaga Commented Aug 8, 2017 at 18:09
  • Your question title has the terms switched – Mangohero1 Commented Aug 8, 2017 at 18:25
  • whoops didn';t realize that will swap – Vincent Tang Commented Aug 8, 2017 at 18:30
Add a ment  | 

2 Answers 2

Reset to default 11

Here is a python translation:

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):
        self.views += 1
        return self.views

You must declare a class, then initialize an instance of that class as follows:

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

Some notable differences are that self is not implicitly available but is actually a required parameter to all methods of the class. However, you should consult the documentation on python classes for more information.

Edit

As of python3.7, I feel obligated to show that newly introduced dataclasses are another (and increasingly mon) way of writing simple classes like this.

from dataclasses import dataclass

@dataclass
class Course:
     title: str 
     instructor: str 
     level: str 
     published: bool
     views: int 

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

There was some errors on the python solution fixed it now

#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())

Result print out

A title

123457

Using print(courseB.update_views) outputs this though,<bound method Course.update_views of <__main__.Course object at 0x7f9f79978908>> , use print(courseB.update_views())

发布评论

评论列表(0)

  1. 暂无评论