使用getter和setter的优点是什么 - 只能获取和设置 - 而不是简单地使用公共字段来存储这些变量?
如果getter和setter做的不仅仅是简单的get/set,我可以非常快地解决这个问题,但我并不是100%清楚如何:
public String foo;
Run Code Online (Sandbox Code Playgroud)
更糟糕的是:
private String foo;
public void setFoo(String foo) { this.foo = foo; }
public String getFoo() { return foo; }
Run Code Online (Sandbox Code Playgroud)
而前者需要很少的样板代码.
这是一个纯Python特定的设计问题:
class MyClass(object):
...
def get_my_attr(self):
...
def set_my_attr(self, value):
...
Run Code Online (Sandbox Code Playgroud)
和
class MyClass(object):
...
@property
def my_attr(self):
...
@my_attr.setter
def my_attr(self, value):
...
Run Code Online (Sandbox Code Playgroud)
Python让我们可以这样做.如果你要设计一个Python程序,你会使用哪种方法?为什么?
如何在Python中创建类或方法摘要?
我试着__new__()像这样重新定义:
class F:
def __new__(cls):
raise Exception("Unable to create an instance of abstract class %s" %cls)
Run Code Online (Sandbox Code Playgroud)
但是现在如果我创建一个G继承自F这样的类:
class G(F):
pass
Run Code Online (Sandbox Code Playgroud)
然后我也无法实例化G,因为它调用了它的超类__new__方法.
有没有更好的方法来定义抽象类?
我有一个朋友喜欢使用元类,并定期提供它们作为解决方案.
我很想你几乎不需要使用元类.为什么?因为我认为如果你正在对一个类做类似的事情,你应该把它做成一个对象.并且需要一个小的重新设计/重构.
能够使用元类导致许多地方的很多人使用类作为某种二流对象,这对我来说似乎是灾难性的.编程是否被元编程取代?遗憾的是,类装饰器的添加使其更加可接受.
所以,我非常想知道Python中元类的有效(具体)用例.或者开悟为什么变异类有时比变异对象更好.
我将开始:
有时,在使用第三方库时,能够以某种方式改变类是有用的.
(这是我能想到的唯一一个案例,并不具体)
我已成功使用Python属性,但我不知道它们是如何工作的.如果我取消引用类之外的属性,我只得到一个类型的对象property:
@property
def hello(): return "Hello, world!"
hello # <property object at 0x9870a8>
Run Code Online (Sandbox Code Playgroud)
但是如果我在一个类中放置一个属性,那么行为就会大不相同:
class Foo(object):
@property
def hello(self): return "Hello, world!"
Foo().hello # 'Hello, world!'
Run Code Online (Sandbox Code Playgroud)
我注意到unbound Foo.hello仍然是property对象,所以类实例化必须做出魔法,但那有什么神奇之处呢?
def make_bold(fn):
return lambda : "<b>" + fn() + "</b>"
def make_italic(fn):
return lambda : "<i>" + fn() + "</i>"
@make_bold
@make_italic
def hello():
return "hello world"
helloHTML = hello()
Run Code Online (Sandbox Code Playgroud)
输出: "<b><i>hello world</i></b>"
我大致了解装饰器以及它在大多数示例中如何与其中一个一起工作.
在这个例子中,有2个.从输出,它似乎@make_italic首先执行,然后@make_bold.
这是否意味着对于装饰函数,它将首先运行函数然后移动到其他装饰器的顶部?就像@make_italic第一个@make_bold,而不是相反.
那么这意味着它与大多数编程中的自上而下方法的规范不同?只为这个装饰器的情况?还是我错了?
我仍然在学习并回答我的一个问题:在这里,我被告知它可能是因为有问题的元素不在视野中.
我查看了文档,所以这里是最相关的答案:这里
您可以使用"org.openqa.selenium.interactions.Actions"类移动到元素:
WebElement element = driver.findElement(By.id("my-id"));
Actions actions = new Actions(driver);
actions.moveToElement(element);
## actions.click();
actions.perform();
Run Code Online (Sandbox Code Playgroud)
当我尝试使用上面的内容滚动到元素时:它说WebElement没有定义.
我想这是因为我没有导入相关模块.有人可以指出我应该导入的内容吗?
编辑:正如alecxe所指出的,这是java代码.
但与此同时,在试图弄清楚它一段时间之后.我找到了WebElement的导入方法:
from selenium.webdriver.remote.webelement import WebElement
Run Code Online (Sandbox Code Playgroud)
可能会帮助像我这样的人.
如何,这也是一个很好的教训,IMO:
去:文档 的
class selenium.webdriver.remote.webelement.WebElement(parent, id_, w3c=False)
Run Code Online (Sandbox Code Playgroud)
需要分成上面提到的命令格式.
给出以下整数和计算
from __future__ import division
a = 23
b = 45
c = 16
round((a/b)*0.9*c)
Run Code Online (Sandbox Code Playgroud)
这导致:
TypeError: 'int' object is not callable.
Run Code Online (Sandbox Code Playgroud)
如何将输出舍入为整数?
我在看内部Python如何实现属性描述符.根据property()描述符协议实现文档,为方便起见,在此处复制它:
class Property(object):
"Emulate PyProperty_Type() in Objects/descrobject.c"
def __init__(self, fget=None, fset=None, fdel=None, doc=None):
self.fget = fget
self.fset = fset
self.fdel = fdel
if doc is None and fget is not None:
doc = fget.__doc__
self.__doc__ = doc
def __get__(self, obj, objtype=None):
if obj is None:
return self
if self.fget is None:
raise AttributeError("unreadable attribute")
return self.fget(obj)
def __set__(self, obj, value):
if self.fset is None:
raise AttributeError("can't set attribute")
self.fset(obj, value)
def __delete__(self, obj):
if self.fdel is …Run Code Online (Sandbox Code Playgroud) class human(object):
def __init__(self, name=''):
self.name = name
@property
def name(self):
return self._name
@name.setter
def name(self, value):
self._name = value
class superhuman(human):
@property
def name(self):
return 'super ' + name
s = superhuman('john')
print s.name
# Doesn't work :( "AttributeError: can't set attribute"
s.name = 'jack'
print s.name
Run Code Online (Sandbox Code Playgroud)
我希望能够覆盖属性,但能够使用超级父级的setter而不必覆盖子类中的setter.
pythonicaly可能吗?
python ×9
properties ×4
getter ×2
inheritance ×2
oop ×2
setter ×2
abstract ×1
abstraction ×1
class ×1
decorator ×1
java ×1
metaclass ×1
mutators ×1
python-2.7 ×1
python-3.x ×1
selenium ×1