小编Mor*_*gan的帖子

同名属性的2个名称

我想知道是否有办法"链接"一个类的两个属性或给同一个属性赋予两个名称?

例如,我实际上正在编写一个脚本,该脚本根据用户提供的数据创建三角形.我的三角形是ABC.这个三角形的边是AB,BC和CA. 所以三角形有这3个属性(self.AB,self.BC,self.CA).但AB = BA所以我想允许用户print myInstance.BA代替print myInstance.AB.

所以我想创建属性self.AB和属性BA(返回self.AB).当我尝试做print myInstance.BA而不是print myInstance.AB但我很贪心时工作正常...

我也想让用户做myInstance.BA = 5而不是myInstance.AB = 5在做这个时也编辑属性AB.

有办法做到这一点吗?

python

8
推荐指数
1
解决办法
339
查看次数

在继承自int或float或str的类中设置参数的值

当我尝试设置继承自的类的参数的值时,会引发错误str.只有当我尝试访问参数时才会发生错误myClass(arg = 'test').错误是:

TypeError: 'arg' is an invalid keyword argument for this function
Run Code Online (Sandbox Code Playgroud)

问题显示在此示例中:

class A(str):    
    def __init__(self, arg):
        pass

A("test") # Works well
A(arg = "test") # Fails
Run Code Online (Sandbox Code Playgroud)

只有最后一行才会引发错误.前一行效果很好.问题与从int或继承的类相同float.

更新(解决方案):

我找到了这些链接的解决方案:

解决方案是:

class A(str):

    def __new__(cls, *args, **kwargs):
        return str.__new__(cls)

    def __init__(self, arg01):
        print(arg01)

A(arg01= "test")
Run Code Online (Sandbox Code Playgroud)

我不确切知道为什么会有效,我会对此进行调查.如果有人有明确的解释,我很感兴趣,我提前感谢他.

更新(我的解释):

我不确定我会说什么,但这就是我所理解的.

想象一下'myClass'没有任何遗产的课程.当我这样做时myInstance = myClass(),会发生以下情况:

该方法myClass.__new__被执行.此方法将创建对象myInstance.__new__是真正的构造函数(__init__不是构造函数!).在伪代码中,__new__看起来像这样:

def __new__ …
Run Code Online (Sandbox Code Playgroud)

python inheritance class python-internals

5
推荐指数
1
解决办法
198
查看次数

最小宽度在 flex-direction: row 和 flex-direction: column 中呈现不同

我有一个带有display: flex和的容器flex-direction: row

在那个容器中有一个子容器,也带有display: flex但带有flex-direction: column

问题是如果我在子容器中添加一个输入min-width,该输入的 将被忽略。

这是我在 flexbox 中尝试几种输入情况的代码:

form {
  margin: 100px;
}
div.flex_ctn {
  display: flex;
}
input {
  flex: 1;
  min-width: 40px;
}
div.column {
  flex-direction: column;
}
div.row {
  flex-direction: row;
}
div.sub_ctn {
  flex: 1;
  /*min-width:40px;*/
}
Run Code Online (Sandbox Code Playgroud)
<form>
  <div class="flex_ctn row">
    <input />
  </div>
  <div class="flex_ctn column">
    <input />
  </div>
  <div class="flex_ctn row">
    <div class="flex_ctn column sub_ctn">
      <input />
    </div>
  </div>
  <div class="flex_ctn …
Run Code Online (Sandbox Code Playgroud)

html css flexbox

4
推荐指数
1
解决办法
2190
查看次数

PYTHON in MAYA: get all attributes

I would like to know if there is a way to get the list of attributes that we can get with pymel.core.getAttr() (or maya.cmds.getAttr() for cmds users). __dict__ doesn't give that list.

import pymel.core as pmc

myCubeTrans, myCubeShape = pmc.polyCube()

>>> print myCubeTrans.__dict__

{'__apiobjects__': {'MDagPath': <maya.OpenMaya.MDagPath; proxy of <Swig Object of type 'MDagPath *' at 0x00000000132ECCC0> >, 'MObjectHandle': <maya.OpenMaya.MObjectHandle; proxy of <Swig Object of type 'MObjectHandle *' at 0x00000000132EC9F0> >, 'MFn': <maya.OpenMaya.MFnTransform; proxy of <Swig Object of type 'MFnTransform *' …
Run Code Online (Sandbox Code Playgroud)

python maya

3
推荐指数
1
解决办法
1万
查看次数

删除搁架中数据的最干净方法是什么?

当我删除架子中的元素时,我还剩下一些数据。我尝试过popdelclear结果是一样的。剩余的数据位于扩展名为 dat 的文件中。因此,即使使用该方法清除后,dat 文件的大小也不是 0Ko。

这就是我尝试过的:

import shelve
test = shelve.open('test')
test['a']=1
#test.pop('a')
#del test['a']
#test.clear()
test.close()
Run Code Online (Sandbox Code Playgroud)

有没有办法彻底删除货架上的数据?

python shelve

2
推荐指数
1
解决办法
4817
查看次数

在某些已定义的情况下,使用类返回False

我想知道是否有办法配置一个在某些特定情况下返回False的类.是什么让空字符串和空列表返回False?如何使用我自己的条件重现该模式(例如,当该类的属性等于定义的值时返回False)?

举例来说明我在寻找的东西:

class Foo():
  def__init__(self, value):
    self.value=value

# I would like that the class Foo return False if his value equal 0.

a = Foo(1)
b = Foo(0)

for each in [a,b]:
  if each : print( "The value isn't 0 :)" )
  else : print( "The value is 0..." )
Run Code Online (Sandbox Code Playgroud)

python

1
推荐指数
1
解决办法
46
查看次数

标签 统计

python ×5

class ×1

css ×1

flexbox ×1

html ×1

inheritance ×1

maya ×1

python-internals ×1

shelve ×1