可能重复:
类__init __()函数内部和外部的变量
我注意到在Python中,人们以两种不同的方式初始化他们的类属性.
第一种方式是这样的:
class MyClass:
__element1 = 123
__element2 = "this is Africa"
def __init__(self):
#pass or something else
Run Code Online (Sandbox Code Playgroud)
另一种风格如下:
class MyClass:
def __init__(self):
self.__element1 = 123
self.__element2 = "this is Africa"
Run Code Online (Sandbox Code Playgroud)
哪个是初始化类属性的正确方法?
我有一个表单输入元素,并希望更改其title属性.这必须很容易,但由于某种原因,我找不到如何做到这一点.这是怎么做的,我应该在哪里以及如何搜索如何做到这一点?
有没有开发使用setAttribute而不是dot(.)属性表示法的最佳实践?
例如:
myObj.setAttribute("className", "nameOfClass");
myObj.setAttribute("id", "someID");
Run Code Online (Sandbox Code Playgroud)
要么
myObj.className = "nameOfClass";
myObj.id = "someID";
Run Code Online (Sandbox Code Playgroud) 可能重复:
通过其描述属性查找枚举值
我有一个通用的扩展方法,它Description从以下方式获取属性Enum:
enum Animal
{
[Description("")]
NotSet = 0,
[Description("Giant Panda")]
GiantPanda = 1,
[Description("Lesser Spotted Anteater")]
LesserSpottedAnteater = 2
}
public static string GetDescription(this Enum value)
{
FieldInfo field = value.GetType().GetField(value.ToString());
DescriptionAttribute attribute
= Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute))
as DescriptionAttribute;
return attribute == null ? value.ToString() : attribute.Description;
}
Run Code Online (Sandbox Code Playgroud)
所以我可以......
string myAnimal = Animal.GiantPanda.GetDescription(); // = "Giant Panda"
Run Code Online (Sandbox Code Playgroud)
现在,我正试图在另一个方向上找出等效函数,比如......
Animal a = (Animal)Enum.GetValueFromDescription("Giant Panda", typeof(Animal));
Run Code Online (Sandbox Code Playgroud) 如何在不添加jQuery值的情况下设置数据属性?我要这个:
<body data-body>
Run Code Online (Sandbox Code Playgroud)
我试过了:
$('body').attr('data-body'); // this is a getter, not working
$('body').attr('data-body', null); // not adding anything
Run Code Online (Sandbox Code Playgroud)
其他所有东西似乎都将第二个参数添加为字符串.是否可以只设置一个没有值的属性?
假设我有一个Python对象x和一个字符串s,我怎么设置属性s上x?所以:
>>> x = SomeObject()
>>> attr = 'myAttr'
>>> # magic goes here
>>> x.myAttr
'magic'
Run Code Online (Sandbox Code Playgroud)
什么是魔术?顺便说一下,这个目的是缓存呼叫x.__getattr__().
没有多少人知道这个功能,但Python的功能(和方法)可以有属性.看吧:
>>> def foo(x):
... pass
...
>>> foo.score = 10
>>> dir(foo)
['__call__', '__class__', '__delattr__', '__dict__', '__doc__', '__get__', '__getattribute__', '__hash__', '__init__', '__module__', '__name__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__', 'func_closure', 'func_code', 'func_defaults', 'func_dict', 'func_doc', 'func_globals', 'func_name', 'score']
>>> foo.score
10
>>> foo.score += 1
>>> foo.score
11
Run Code Online (Sandbox Code Playgroud)
Python中此功能的可能用途和滥用情况是什么?我知道的一个好用途是PLY使用docstring将语法规则与方法相关联.但是自定义属性呢?有充分的理由使用它们吗?
我想这样做:
ng-hide="!globals.isAdmin && mapping.is_default"
Run Code Online (Sandbox Code Playgroud)
但表达式总是评估false.
我不想定义特殊功能$scope.
[dcl.attr.noreturn]提供以下示例:
[[ noreturn ]] void f() {
throw "error";
// OK
}
Run Code Online (Sandbox Code Playgroud)
但我不明白有什么意义[[noreturn]],因为函数的返回类型已经存在void.
那么,该noreturn属性的重点是什么?它应该如何使用?
我有一个多项目的解决方案.我试图通过链接一个解决方案范围的程序集信息文件来优化AssemblyInfo.cs文件.这样做的最佳做法是什么?哪些属性应该在解决方案范围的文件中,哪些属于项目/程序集特定?
编辑:如果您有兴趣,有一个后续问题AssemblyVersion,AssemblyFileVersion和AssemblyInformationalVersion之间有什么区别?
attributes ×10
python ×3
.net ×2
javascript ×2
jquery ×2
angularjs ×1
assemblies ×1
assemblyinfo ×1
attr ×1
c# ×1
c++ ×1
c++11 ×1
class ×1
enums ×1
expression ×1
forms ×1
function ×1
noreturn ×1
object ×1
setattribute ×1