GVKun编程网logo

为什么要使用 Ruby 的 attr_accessor、attr_reader 和 attr_writer?(为什么要typedef int elemtype)

13

本文的目的是介绍为什么要使用Ruby的attr_accessor、attr_reader和attr_writer?的详细情况,特别关注为什么要typedefintelemtype的相关信息。我们将通过

本文的目的是介绍为什么要使用 Ruby 的 attr_accessor、attr_reader 和 attr_writer?的详细情况,特别关注为什么要typedef int elemtype的相关信息。我们将通过专业的研究、有关数据的分析等多种方式,为您呈现一个全面的了解为什么要使用 Ruby 的 attr_accessor、attr_reader 和 attr_writer?的机会,同时也不会遗漏关于AttributeError: 'Lines_LineSeries_LineIterator_DataAccessor_Strateg' 对象没有属性 'analyers'、attr_accessible 就是白名单、attr_accessor 和 attr_accessible 的区别、Python __setattr__、 __getattr__、 __delattr__、__call__用法的知识。

本文目录一览:

为什么要使用 Ruby 的 attr_accessor、attr_reader 和 attr_writer?(为什么要typedef int elemtype)

为什么要使用 Ruby 的 attr_accessor、attr_reader 和 attr_writer?(为什么要typedef int elemtype)

Ruby 有这种方便快捷的方式来共享实例变量,方法是使用类似的键

attr_accessor :varattr_reader :varattr_writer :var

我为什么要选择attr_reader或者attr_writer我可以简单地使用attr_accessor?有没有类似性能的东西(我怀疑)?我想这是有原因的,否则他们不会制造这样的钥匙。

答案1

小编典典

您可以使用不同的访问器将您的意图传达给阅读您的代码的人,并使编写无论如何调用公共 API 都能正常工作的类变得更容易。

class Person  attr_accessor :age  ...end

在这里,我可以看到我可以读和写的年龄。

class Person  attr_reader :age  ...end

在这里,我可以看到我可能只阅读年龄。想象一下,它是由这个类的构造函数设置的,之后保持不变。如果有一个年龄的修改器(编写器),并且假设年龄一旦设置就不会改变,那么编写该类时,调用该修改器的代码可能会导致错误。

但是幕后发生了什么?

如果你写:

attr_writer :age

这被翻译成:

def age=(value)  @age = valueend

如果你写:

attr_reader :age

这被翻译成:

def age  @ageend

如果你写:

attr_accessor :age

这被翻译成:

def age=(value)  @age = valueenddef age  @ageend

知道了这一点,这里有另一种思考方式:如果你没有 attr_…
帮助器,并且必须自己编写访问器,你会编写比你的类需要更多的访问器吗?例如,如果只需要读取年龄,您是否还会编写一个允许写入的方法?

AttributeError: 'Lines_LineSeries_LineIterator_DataAccessor_Strateg' 对象没有属性 'analyers'

AttributeError: 'Lines_LineSeries_LineIterator_DataAccessor_Strateg' 对象没有属性 'analyers'

如何解决AttributeError: ''Lines_LineSeries_LineIterator_DataAccessor_Strateg'' 对象没有属性 ''analyers''?

我在互联网上搜索了这个问题,但没有找到任何合适的答案,所以我发布了这个问题。我的代码在此 link 中。

错误:

AttributeError: ''Lines_Lineseries_LineIterator_DataAccessor_Strateg'' object has no attribute ''analyzers''

错误实际上来自文件lineseries.py中的这一行,该文件在安装时随pycharm一起提供:

return getattr(self.lines,name)

如果您想查看完整代码,它在此link 中。 我去了搜索中出现的每个链接,但没有一个有帮助。我尝试按照 Github 上某人的建议删除上一行中的“self”,但这没有用。

感谢大家的帮助。我知道我的代码很长,很抱歉。

注意:我使用的是 Windows 10,我的系统上安装了 python 3.7 版本。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

attr_accessible 就是白名单

attr_accessible 就是白名单

在 active_record 的 validators 里有 validates_associated 方法,校验关联的,还没用过? 关联表得 belongs_to 两个表

validates_uniqueness_of ... :case_sensitive => false
# 大小写不敏感 ''jerry''''JERRY'' 被认为是重名

預設是 true,表示要區分大小寫
validates_uniqueness_of ... :case_sensitive => true
# 大小写敏感 ''jerry''''JERRY'' 被认为不是重名

数组有一很有趣的方法 rotate(cnt=1) → new_ary click to toggle source 旋转,将前面的旋

attr_accessor 和 attr_accessible 的区别

attr_accessor 和 attr_accessible 的区别

attr_accessor在 Rails 中,和 和有什么区别attr_accessible?据我了解,
usingattr_accessor用于为该变量创建 getter 和 setter
方法,以便我们可以访问变量,如Object.variableor Object.variable = some_value

我读到这attr_accessible使得外部世界可以访问该特定变量。有人可以告诉我有什么区别吗

Python __setattr__、 __getattr__、 __delattr__、__call__用法

Python __setattr__、 __getattr__、 __delattr__、__call__用法

getattr

`getattr`函数属于内建函数,可以通过函数名称获取
代码如下:
value = obj.attribute
value = getattr(obj, “attribute”)

使用`getattr`来实现工厂模式
代码如下:
#一个模块支持html、text、xml等格式的打印,根据传入的formate参数的不同,调用不同的函数实现几种格式的输出

import statsout

def output(data, format=”text”):
output_function = getattr(statsout, “output_%s” %format)
return output_function(data)

__call__

`__call__`方法用于实例自身的调用:
代码如下:
class storage(dict):
# __call__方法用于实例自身的调用
#达到()调用的效果
def __call__ (self, key):
try:
return self[key]
except KeyError, k:
return None

s = storage()
s[‘key’] = ‘value’
print s(key) #调用__call__

__getattr__

从对象中读取某个属性时,首先需要从self.__dicts__中搜索该属性,再从__getattr__中查找。

代码如下:
class A(object):
def __init__(self):
self.name = ‘from __dicts__: zdy’

def __getattr__(self, item):
if item == ‘name’:
return ‘from __getattr__: zdy’
elif item == ‘age’:
return 26

a = A()
print a.name # 从__dict__里获得的
print a.age # 从__getattr__获得的

__setattr__

`__setattr__`函数是用来设置对象的属性,通过object中的__setattr__函数来设置属性:
代码如下:
class A(object):
def __setattr__(self, *args, **kwargs):
print ‘call func set attr’
return object.__setattr__(self, *args, **kwargs)

__delattr__

`__delattr__`函数式用来删除对象的属性:
代码如下:
class A(object):
def __delattr__(self, *args, **kwargs):
print ‘call func del attr’
return object.__delattr__(self, *args, **kwargs)

例子

完整例子可以参考微博API:http://github.liaoxuefeng.com/sinaweibopy/
代码如下:
class _Executable(object):

def __init__(self, client, method, path):
self._client = client
self._method = method
self._path = path
#__call__函数实现_Executable函数对象为可调用的
def __call__(self, **kw):
method = _METHOD_MAP[self._method]
if method==_HTTP_POST and ‘pic’ in kw:
method = _HTTP_UPLOAD
return _http_call(‘%s%s.json’ % (self._client.api_url, self._path), method, self._client.access_token, **kw)

def __str__(self):
return ‘_Executable (%s %s)’ % (self._method, self._path)

__repr__ = __str__

class _Callable(object):

def __init__(self, client, name):
self._client = client
self._name = name

def __getattr__(self, attr):
if attr==’get’:
       #初始化_Executable对象,调用__init__函数
return _Executable(self._client, ‘GET’, self._name)
if attr==’post’:
return _Executable(self._client, ‘POST’, self._name)
name = ‘%s/%s’ % (self._name, attr)
return _Callable(self._client, name)

def __str__(self):
return ‘_Callable (%s)’ % self._name

__repr__ = __str__

而在源码中,存在下面代码片段:

代码如下:
class APIClient(object):
”’
API client using synchronized invocation.
”’

def __getattr__(self, attr):
if ‘__’ in attr:
return getattr(self.get, attr)
return _Callable(self, attr)

因此,加入我们初始化对象,并调用某函数如下:
代码如下:
client = APIClient(…)
#会调用__getattr__函数,从而调用__call__函数
client.something.get()

今天的关于为什么要使用 Ruby 的 attr_accessor、attr_reader 和 attr_writer?为什么要typedef int elemtype的分享已经结束,谢谢您的关注,如果想了解更多关于AttributeError: 'Lines_LineSeries_LineIterator_DataAccessor_Strateg' 对象没有属性 'analyers'、attr_accessible 就是白名单、attr_accessor 和 attr_accessible 的区别、Python __setattr__、 __getattr__、 __delattr__、__call__用法的相关知识,请在本站进行查询。

本文标签: