- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
在 Javascript 中,有多种方法可以继承方法。下面是使用几种方法的混合示例:
A = {
name: 'first',
wiggle: function() { return this.name + " is wiggling" },
shake: function() { return this.name + " is shaking" }
}
B = Object.create(A)
B.name = 'second'
B.bop = function() { return this.name + ' is bopping' }
C = function(name) {
obj = Object.create(B)
obj.name = name
obj.crunk = function() { return this.name + ' is crunking'}
return obj
}
final = new C('third')
这为我提供了以下继承层次结构。
需要注意的重要事项之一是 name
每个对象的属性。当运行一个方法时,即使在原型(prototype)链的最下游,由 this
定义的本地上下文关键字确保使用 localmost 属性/变量。
我最近转向使用 Python,但我无法理解子类如何访问父类(super class)方法,以及变量作用域/对象属性如何工作。
我在 Scrapy 中创建了一个 Spider,它(非常成功地)在一个域上抓取了 2000 多个页面并将它们解析为我需要的格式。许多助手只在主程序中运行 parse_response
方法,我可以直接在数据上使用。原来的蜘蛛看起来像这样:
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from spider_scrape.items import SpiderItems
class ScrapeSpider(CrawlSpider):
name = "myspider"
allowed_domains = ["domain.com.au"]
start_urls = ['https://www.domain.com.au/']
rules = (Rule(SgmlLinkExtractor(allow=()),
callback="parse_items",
follow=True), )
def parse_items(self, response):
...
回调函数 parse_items 包含为我处理响应的逻辑。当我概括所有内容时,我得到了以下结果(目的是在多个域中使用它):
#Base
class BaseSpider(CrawlSpider):
"""Base set of configuration rules and helper methods"""
rules = (Rule(LinkExtractor(allow=()),
callback="parse_response",
follow=True),)
def parse_response(self, response):
...
def clean_urls(string):
"""remove absolute URL's from hrefs, if URL is form an external domain do nothing"""
for domain in allowed_domains:
string = string.replace('http://' + domain, '')
string = string.replace('https://' + domain, '')
if 'http' not in string:
string = "/custom/files" + string
return string
#Specific for each domain I want to crawl
class DomainSpider(BaseSpider):
name = 'Domain'
allowed_domains = ['Domain.org.au']
start_urls = ['http://www.Domain.org.au/'
,'http://www.Domain.org.au/1']
当我通过 Scrapy 命令行运行它时,控制台出现以下错误:
经过一些测试后,将列表推导式更改为此使其工作:for domain in self.allowed_domains:
很好,这看起来与 this
非常相似Javascript 中的关键字 - 我使用对象的属性来获取值。还有更多的变量/属性将保存抓取所需的 XPath 表达式:
class DomainSpider(BaseSpider):
name = 'Domain'
page_title = '//title'
page_content = '//div[@class="main-content"]'
更改 Spider 的其他部分以模仿 allowed_domains 变量的部分,我收到此错误:
我尝试以几种不同的方式设置属性,包括使用 self.page_content
和/或 __init__(self)
构造函数没有成功,但有不同的错误。
我完全不知道这里发生了什么。我期望发生的行为是:
scrapy crawl <spider name>
从终端实例化 DomainSpider 类this
关键词如果有人能
提前致谢。
最佳答案
我不熟悉 JavaScript,但与您的问题类似的问题总是包含一个答案,表明您必须学习使用 Python 的方法,而不是试图强制 Python 像您的其他语言。尝试在 Python 中重新创建您的 Javascript 风格,我想到了这个:
class A(object):
def __init__(self):
self.name = 'first'
def wiggle(self):
return self.name + ' is wiggling'
def shake(self):
return self.name + ' is shaking'
创建 A
的实例,更改其名称并添加方法属性到实例
b = A()
b.name = 'second'
b.bop = lambda : b.name + ' is bopping'
返回 A
实例的函数,带有附加属性 crunk
。我不认为你的例子是这样,thing
不会有 bop
方法,尽管函数中的另一个语句可以添加一个。
def c(name):
thing = A()
thing.name = name
thing.crunk = lambda : thing.name + ' is crunking'
return thing
final = c('third')
没有任何继承正在进行,只有 A
的实例具有额外的属性。您得到以下结果:
>>>
>>> b.name
'second'
>>> b.bop()
'second is bopping'
>>> b.shake()
'second is shaking'
>>> b.wiggle()
'second is wiggling'
>>>
>>> final.name
'third'
>>> final.crunk()
'third is crunking'
>>> final.shake()
'third is shaking'
>>> final.wiggle()
'third is wiggling'
>>> final.bop()
Traceback (most recent call last):
File "<pyshell#32>", line 1, in <module>
final.bop()
AttributeError: 'A' object has no attribute 'bop'
>>>
在 Python 中你会这样做:
A
类具有 name
属性的默认参数和两个将绑定(bind)到 A
实例的方法。 name
是一个instance 属性,因为它是在__init__
中定义的。 A
的 instances 将具有 name
属性 - A.name
将引发 AttributeError。
class A(object):
def __init__(self, name = 'first'):
self.name = name
def wiggle(self):
return self.name + ' is wiggling'
def shake(self):
return self.name + ' is shaking'
Foo
继承了 A
的所有内容并定义了一个附加属性 bop
。
class Foo(A):
def bop(self):
return self.name + ' is bopping'
Bar
继承了 Foo
的所有内容并定义了一个附加属性 crunk
class Bar(Foo):
def crunk(self):
return self.name + ' is crunking'
Baz
继承了 Bar
的所有内容并覆盖了 wiggle
class Baz(Bar):
def wiggle(self):
return 'This Baz instance, ' + self.name + ', is wiggling'
foo = Foo('second')
bar = Bar('third')
baz = Baz('fourth')
用法:
>>>
>>> foo.name
'second'
>>> foo.bop()
'second is bopping'
>>> foo.shake()
'second is shaking'
>>> foo.wiggle()
'second is wiggling'
>>>
>>> bar.name
'third'
>>> bar.bop()
'third is bopping'
>>> bar.shake()
'third is shaking'
>>> bar.wiggle()
'third is wiggling'
>>> bar.crunk()
'third is crunking'
>>>
>>> baz.wiggle()
'This Baz instance, fourth, is wiggling'
>>>
这些示例中的类具有仅对类实例有效的方法属性 - 方法需要绑定(bind)到实例。我没有包含任何不需要绑定(bind)到实例的类方法或静态方法的示例 - What is the difference between @staticmethod and @classmethod in Python? 有一些很好的答案
>>> A.wiggle
<unbound method A.wiggle>
>>> A.wiggle()
Traceback (most recent call last):
File "<pyshell#41>", line 1, in <module>
A.wiggle()
TypeError: unbound method wiggle() must be called with A instance as first argument (got nothing instead)
>>> Bar.crunk
<unbound method Bar.crunk>
>>> Bar.crunk()
Traceback (most recent call last):
File "<pyshell#43>", line 1, in <module>
Bar.crunk()
TypeError: unbound method crunk() must be called with Bar instance as first argument (got nothing instead)
>>>
关于python - 从 Javascript 到 Python - 了解类、方法和属性的工作原理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27664912/
我有一个 html 格式的表单: 我需要得到 JavaScript在value input 字段执行,但只能通过表单的 submit .原因是页面是一个模板所以我不控制它(不能有
我管理的论坛是托管软件,因此我无法访问源代码,我只能向页面添加 JavaScript 来实现我需要完成的任务。 我正在尝试用超链接替换所有页面上某些文本关键字的第一个实例。我还根据国家/地区代码对这些
我正在使用 JS 打开新页面并将 HTML 代码写入其中,但是当我尝试使用 document.write() 在新页面中编写 JS 时功能不起作用。显然,一旦看到 ,主 JS 就会关闭。用于即将打开的
提问不是为了解决问题,提问是为了更好地理解系统 专家!我知道每当你将 javascript 代码输入 javascript 引擎时,它会立即由 javascript 引擎执行。由于没有看过Engi
我在一个文件夹中有两个 javascript 文件。我想将一个变量的 javascript 文件传递到另一个。我应该使用什么程序? 最佳答案 window.postMessage用于跨文档消息。使
我有一个练习,我需要输入两个输入并检查它们是否都等于一个。 如果是 console.log 正则 console.log false 我试过这样的事情: function isPositive(fir
我正在做一个Web应用程序,计划允许其他网站(客户端)在其页面上嵌入以下javascript: 我的网络应用程序位于 http://example.org 。 我不能假设客户端网站的页面有 JQue
目前我正在使用三个外部 JS 文件。 我喜欢将所有三个 JS 文件合而为一。 尽一切可能。我创建 aio.js 并在 aio.js 中 src="https://code.jquery.com/
我有例如像这样的数组: var myArray = []; var item1 = { start: '08:00', end: '09:30' } var item2 = {
所以我正在制作一个 Chrome 扩展,它使用我制作的一些 TamperMonkey 脚本。我想要一个“主”javascript 文件,您可以在其中包含并执行其他脚本。我很擅长使用以下行将其他 jav
我有 A、B html 和 A、B javascript 文件。 并且,如何将 A JavaScript 中使用的全局变量直接移动到 B JavaScript 中? 示例 JavaScript) va
我需要将以下整个代码放入名为 activate.js 的 JavaScript 中。你能告诉我怎么做吗? var int = new int({ seconds: 30, mark
我已经为我的 .net Web 应用程序创建了母版页 EXAMPLE1.Master。他们的 I 将值存储在 JavaScript 变量中。我想在另一个 JS 文件中检索该变量。 示例1.大师:-
是否有任何库可以用来转换这样的代码: function () { var a = 1; } 像这样的代码: function () { var a = 1; } 在我的浏览器中。因为我在 Gi
我收到语法缺失 ) 错误 $(document).ready(function changeText() { var p = document.getElementById('bidp
我正在制作进度条。它有一个标签。我想调整某个脚本完成的标签。在找到可能的解决方案的一些答案后,我想出了以下脚本。第一个启动并按预期工作。然而,第二个却没有。它出什么问题了?代码如下: HTML:
这里有一个很简单的问题,我简单的头脑无法回答:为什么我在外部库中加载时,下面的匿名和onload函数没有运行?我错过了一些非常非常基本的东西。 Library.js 只有一行:console.log(
我知道 javascript 是一种客户端语言,但如果实际代码中嵌入的 javascript 代码以某种方式与在控制台上运行的代码不同,我会尝试找到答案。让我用一个例子来解释它: 我想创建一个像 Mi
我如何将这个内联 javascript 更改为 Unobtrusive JavaScript? 谢谢! 感谢您的回答,但它不起作用。我的代码是: PHP js文件 document.getElem
我正在寻找将简单的 JavaScript 对象“转储”到动态生成的 JavaScript 源代码中的最优雅的方法。 目的:假设我们有 node.js 服务器生成 HTML。我们在服务器端有一个对象x。
我是一名优秀的程序员,十分优秀!