is it a bug ?

S

Sylvain Ferriol

hello
can you explain to me what happened:

class toto(object):
eq = float.__eq__

t = toto()

getattr(t,'eq')
TypeError: descriptor '__eq__' for 'float' objects doesn't apply to
'toto' object
 
P

Peter Otten

Sylvain said:
can you explain to me what happened:

class toto(object):
eq = float.__eq__

t = toto()

getattr(t,'eq')
TypeError: descriptor '__eq__' for 'float' objects doesn't apply to
'toto' object

float.__eq__ is probably implemented in C and its operation will make sense
only for instances of float or subclasses of float. Try
.... eq = float.__eq__
....False

Peter
 
F

Fredrik Lundh

Sylvain said:
can you explain to me what happened:

class toto(object):
eq = float.__eq__

t = toto()

getattr(t,'eq')
TypeError: descriptor '__eq__' for 'float' objects doesn't apply to
'toto' object

I'd say the error message explains it pretty well. what did you expect
this to do?

</F>
 
S

Sylvain Ferriol

Fredrik Lundh a écrit :
I'd say the error message explains it pretty well. what did you expect
this to do?
i just want a class variable named 'eq', i could understand if its name
was '__eq__'.
class toto(object):
__eq__ = float.__eq__

why i can not create some class variables like this ?
class toto(object):
a= float.__eq__
b= str.__eq__
......
 
S

Sylvain Ferriol

Peter Otten a écrit :
Sylvain Ferriol wrote:




float.__eq__ is probably implemented in C and its operation will make sense
only for instances of float or subclasses of float. Try



.... eq = float.__eq__
....


False
i can not use it because:
class toto(float):
def __init__(self,a=None):pass

t=toto(a=3)
TypeError: 'a' is an invalid keyword argument for this function
 
F

Fredrik Lundh

Sylvain said:
i just want a class variable named 'eq'

so why are you assigning another class' descriptor to it? descriptors
are bound to types, and only works properly if used with the type they
were created for.
why i can not create some class variables like this ?
>
class toto(object):
a= float.__eq__
b= str.__eq__

but given that "toto" is neither a float nor a str, what do you expect
that code to *do*?

is this some "programming by random mutation" exercise?

</F>
 
P

Peter Otten

Sylvain said:
i can not use it because:
class toto(float):
def __init__(self,a=None):pass

t=toto(a=3)
TypeError: 'a' is an invalid keyword argument for this function

Override __new__() then:
.... def __new__(cls, a=None):
.... if a is None:
.... a = 42
.... return float.__new__(cls, a)
.... def __init__(cls, a=None):
.... print "random message containing", a
....random message containing None
42.0random message containing 42
42.0

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top