对象默认值 [英] Object default value

查看:67
本文介绍了对象默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将默认值与python对象相关联?即

标记一个属性,使得

对象的赋值运算符返回默认属性而不是对象本身,但是

调用其他对象属性是否正确解析? (我不认为

所以,但我不确定)


示例:


class obj(对象):

x = 1 #assume x以某种方式被设为默认值

y = 2


理想情况下这应该是结果:


myobj = obj()

print myobj# - > 1

打印myobj.y# - > 2

x = myobj

print x# - > 1

打印类型(x)#int


到目前为止,这是我的半工作解决方案:


1)在班级中使用__get__描述符,


def __get __(self,parent,parenttype):return self.x


then


打印myobj #works

打印myobj.y #does不起作用!相当于:print 1.y


2)使用没有__get__描述符的__call__方法。


def __call __(self):return self .x


然后:


打印myobj()#works,但不一样:print myobj

打印myobj.y #works

Is it possible to have a default value associated python objects? I.e.
to flag an attribute in such a way that the assignment operator for the
object returns the default attribute instead of the object itself, but
calls to other object attributes are properly resolved? (I don''t think
so, but I am not sure)

Example:

class obj(object):
x=1 #assume x is somehow made into a default value
y=2

Ideally this should be the result:

myobj=obj()
print myobj #-> 1
print myobj.y #-> 2
x=myobj
print x #-> 1
print type(x) #int

Those are my half-working solutions so far:

1) using __get__ descriptor within the class,

def __get__(self,parent,parenttype): return self.x

then

print myobj #works
print myobj.y #does not work! equivalent to: print 1.y

2) Use a __call__ method without a __get__ descriptor.

def __call__(self): return self.x

then:

print myobj() #works, but not the same as: print myobj
print myobj.y #works

推荐答案

可以通过在
$ b上定义__str__方法来完成打印$ b class,但我不认为你会得到类型(obj)工作

你想要的方式。


class obj(object ):

__default = 1

y = 2

def __str __(个体经营):

返回str(self .__ default)


myobj = obj()

print" myobj =" ;, myobj

print" ; myobj.y =",myobj.y
The prints can be done by defining an __str__ method on the
class, but I don''t think you will get the type(obj) to work
the way you want.

class obj(object):
__default=1
y=2

def __str__(self):
return str(self.__default)

myobj=obj()
print "myobj=", myobj
print "myobj.y=", myobj.y
myobj = 1
myobj.y = 2
myobj= 1
myobj.y= 2



ago写道:是否有可能有一个默认值assoc iated python对象? I.e.
以这样的方式标记属性,即
对象的赋值运算符返回默认属性而不是对象本身,但是对其他对象属性的调用是否正确解析? (我不认为
所以,但我不确定)

例如:

类obj(对象):
x = 1 #assume x以某种方式被设为默认值
y = 2

理想情况下这应该是结果:

myobj = obj()
打印myobj# - > 1
打印myobj.y# - > 2
x = myobj
print x# - > 1
打印类型(x)#int

这些是我到目前为止的半工作解决方案:

1)在类中使用__get__描述符,

def __get __(self,parent,parenttype):return self.x



打印myobj #works
打印myobj.y #does不行!相当于:print 1.y

2)使用没有__get__描述符的__call__方法。

def __call __(self):return self.x

打印myobj()#works,但不一样:print myobj
print myobj.y #works


ago wrote: Is it possible to have a default value associated python objects? I.e.
to flag an attribute in such a way that the assignment operator for the
object returns the default attribute instead of the object itself, but
calls to other object attributes are properly resolved? (I don''t think
so, but I am not sure)

Example:

class obj(object):
x=1 #assume x is somehow made into a default value
y=2

Ideally this should be the result:

myobj=obj()
print myobj #-> 1
print myobj.y #-> 2
x=myobj
print x #-> 1
print type(x) #int

Those are my half-working solutions so far:

1) using __get__ descriptor within the class,

def __get__(self,parent,parenttype): return self.x

then

print myobj #works
print myobj.y #does not work! equivalent to: print 1.y

2) Use a __call__ method without a __get__ descriptor.

def __call__(self): return self.x

then:

print myobj() #works, but not the same as: print myobj
print myobj.y #works



print语句仅用于说明目的,当调用

varx = myobj我需要接收obj.x而不是obj的实例,

但我还需要调用vary = myobj.y。对于

com对象/ VB存在类似的东西,例如excel范围对象使用值作为

默认属性,这样就可以写出


设置rng =范围(...);

x = rng

y = rng.value

''x = = y

z = rng.attributeXYZ

The print statement was only for illustrative purposes, when calling
varx=myobj I need to receive obj.x as opposed to the instance of obj,
but I also need to call vary=myobj.y. Something like that exists for
com objects/VB, for instance an excel range object uses value as the
default attribute, so that you can write

set rng=range(...);
x=rng
y=rng.value
''x==y
z=rng.attributeXYZ


我想你想在这里重载赋值运算符。我不确定在python中是否允许

(我从未见过它)。你可以重载

等于,lt,gt,le,ge运算符(==,<,...)这样的


类Thing:

x = 5

def __str __(自我):

返回str(self.x)

def __eq __( self,other):

if hasattr(其他,''x''):

返回self.x == other.x

否则:

返回self.x ==其他


py> athing = Thing()

py> athing.x

5

py> bob = athing.x

py> athing == bob#具有重叠的等于athing

True

py> athing是bob#不是同一个对象

False


但是我不认为在python中允许赋值重载:

py> athing = Thing()

py>打印athing#重载__str __()

5

py> bob = athing

py> type(bob)#不会是int ==> python语言约束

< type''instance''>


James


9月20日星期二2005 13:05,前写道:
I think you want to overload the assignment operator here. I''m not sure that
is allowed in python (I''ve never seen it done). You can overload the
equality, lt, gt, le, ge operators (==, <, ...) such that

class Thing:
x = 5
def __str__(self):
return str(self.x)
def __eq__(self, other):
if hasattr(other, ''x''):
return self.x == other.x
else:
return self.x == other

py> athing = Thing()
py> athing.x
5
py> bob = athing.x
py> athing == bob # having overlaoded equality for athing
True
py> athing is bob # not the same object
False

But I don''t think assignment overloading is allowed in python:

py> athing = Thing()
py> print athing # having overloaded __str__()
5
py> bob = athing
py> type(bob) # will not be an int ==> python language constraint
<type ''instance''>

James

On Tuesday 20 September 2005 13:05, ago wrote:
print语句仅用于说明目的,当调用
varx = myobj时我需要接收obj.x而不是obj的实例,<但我还需要调用vary = myobj.y。对于
com对象/ VB存在类似的东西,例如excel范围对象使用值作为
默认属性,这样就可以写出设置rng = range(。 ..);
x = rng
y = rng.value
''x == y
z = rng.attributeXYZ
The print statement was only for illustrative purposes, when calling
varx=myobj I need to receive obj.x as opposed to the instance of obj,
but I also need to call vary=myobj.y. Something like that exists for
com objects/VB, for instance an excel range object uses value as the
default attribute, so that you can write

set rng=range(...);
x=rng
y=rng.value
''x==y
z=rng.attributeXYZ



-

James Stroud

加州大学洛杉矶分校基因组学和蛋白质组学研究所

专栏951570

洛杉矶,CA 90095

http://www.jamesstroud.com /


这篇关于对象默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆