473,397 Members | 1,949 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

derived classes and __getattr__

i'm facing the following problem:

class Base(object):
def __getattr__(self, attr): return lambda x: attr + '_' + x

def dec(callable):
return lambda *args: 'dec_' + callable(*args)

class Derived(Base):
what_so_ever = dec(Base.what_so_ever) # wrong, base doesn't have
what_so_ever
mumu = dec(Base.mumu) # wrong, base
doesn't have mumu

any idea how to do this?
Sep 5 '08 #1
7 1696
Alexandru Moșoi wrote:
i'm facing the following problem:

class Base(object):
def __getattr__(self, attr): return lambda x: attr + '_' + x

def dec(callable):
return lambda *args: 'dec_' + callable(*args)

class Derived(Base):
what_so_ever = dec(Base.what_so_ever) # wrong, base doesn't have
what_so_ever
mumu = dec(Base.mumu) # wrong, base
doesn't have mumu

any idea how to do this?
__getattr__() is defined in the class to create instance attributes on the
fly. If you want class attributes you have to put the __getattr__() method
into the class of the class, or "metaclass":

class Base(object):
class __metaclass__(type):
def __getattr__(self, attr):
return lambda self, x: attr + '_' + x

def dec(callable):
return lambda *args: 'dec_' + callable(*args)

class Derived(Base):
what_so_ever = dec(Base.what_so_ever)

d = Derived()
print d.what_so_ever("42")

I don't see how you can turn this into something useful...

Peter
Sep 5 '08 #2
Alexandru Mos,oi wrote:
i'm facing the following problem:

class Base(object):
def __getattr__(self, attr): return lambda x: attr + '_' + x

def dec(callable):
return lambda *args: 'dec_' + callable(*args)

class Derived(Base):
what_so_ever = dec(Base.what_so_ever) # wrong, base doesn't have
what_so_ever
mumu = dec(Base.mumu) # wrong, base
doesn't have mumu

any idea how to do this?
ever thought of using Python to write Python programs?

</F>

Sep 5 '08 #3
On Sep 5, 11:47Â*am, Peter Otten <__pete...@web.dewrote:
Alexandru Moșoi wrote:
i'm facing the following problem:
class Base(object):
Â* def __getattr__(self, attr): return lambda x: attr + '_' + x
def dec(callable):
Â* return lambda *args: 'dec_' + callable(*args)
class Derived(Base):
Â* Â*what_so_ever = dec(Base.what_so_ever) # wrong, base doesn't have
what_so_ever
Â* Â*mumu = dec(Base.mumu) Â* Â* Â* Â* Â*Â* Â* Â* Â* Â* Â* Â* Â*# wrong, base
doesn't have mumu
any idea how to do this?

__getattr__() is defined in the class to create instance attributes on the
fly. If you want class attributes you have to put the __getattr__() method
into the class of the class, or "metaclass":

class Base(object):
Â* Â* class __metaclass__(type):
Â* Â* Â* Â* def __getattr__(self, attr):
Â* Â* Â* Â* Â* Â* return lambda self, x: attr + '_' + x

def dec(callable):
Â* Â* return lambda *args: 'dec_' + callable(*args)

class Derived(Base):
Â* Â*what_so_ever = dec(Base.what_so_ever)

d = Derived()
print d.what_so_ever("42")

I don't see how you can turn this into something useful...

Peter
10x. it works. however I have another small problem. now,
d.third('blah') doesn't work because instance d doesn't have 'third'
attribute. I was expecting derived class to inherit the metaclass as
well, but it didn't.
Sep 5 '08 #4
Alexandru Mosoi wrote:
On Sep 5, 11:47Â*am, Peter Otten <__pete...@web.dewrote:
>Alexandru Moșoi wrote:
i'm facing the following problem:
class Base(object):
def __getattr__(self, attr): return lambda x: attr + '_' + x
def dec(callable):
return lambda *args: 'dec_' + callable(*args)
class Derived(Base):
what_so_ever = dec(Base.what_so_ever) # wrong, base doesn't have
what_so_ever
mumu = dec(Base.mumu) Â* Â* Â* Â* Â* Â* Â* Â* Â* Â* Â* Â* Â*# wrong, base
doesn't have mumu
any idea how to do this?

__getattr__() is defined in the class to create instance attributes on
the fly. If you want class attributes you have to put the __getattr__()
method into the class of the class, or "metaclass":

class Base(object):
class __metaclass__(type):
def __getattr__(self, attr):
return lambda self, x: attr + '_' + x

def dec(callable):
return lambda *args: 'dec_' + callable(*args)

class Derived(Base):
what_so_ever = dec(Base.what_so_ever)

d = Derived()
print d.what_so_ever("42")

I don't see how you can turn this into something useful...

Peter

10x. it works. however I have another small problem. now,
d.third('blah') doesn't work because instance d doesn't have 'third'
attribute. I was expecting derived class to inherit the metaclass as
well, but it didn't.
That has nothing to do with inheritance:
>>type(Derived)
<class 'classattr.__metaclass__'>

If Python doesn't find an attribute in the instance it looks it up in the
class but not in the metaclass:
>>Base.third
<function <lambdaat 0x2b5f8028aa28>
>>Base().third
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'Base' object has no attribute 'third'

I think you should go back to the drawing board now and look for a simpler
approach to solve your actual problem...

Peter
Sep 5 '08 #5
On Sep 5, 11:46*am, Alexandru Mosoi <brtz...@gmail.comwrote:
I have another small problem. now,
d.third('blah') doesn't work because instance d doesn't have 'third'
attribute. I was expecting derived class to inherit the metaclass as
well, but it didn't.
Yep, it is explained here: https://www.ibm.com/developerworks/l...ary/l-pymeta2/
It would help if you told us the real problem you are trying to solve.
Sep 5 '08 #6
On Sep 5, 1:13Â*pm, Peter Otten <__pete...@web.dewrote:
Alexandru Â*Mosoi wrote:
On Sep 5, 11:47Â*am, Peter Otten <__pete...@web.dewrote:
Alexandru Moșoi wrote:
i'm facing the following problem:
class Base(object):
def __getattr__(self, attr): return lambda x: attr + '_' + x
def dec(callable):
return lambda *args: 'dec_' + callable(*args)
class Derived(Base):
what_so_ever = dec(Base.what_so_ever) # wrong, base doesn't have
what_so_ever
mumu = dec(Base.mumu) Â* Â* Â* Â* Â* Â* Â* Â* Â* Â* Â* Â* Â*# wrong, base
doesn't have mumu
any idea how to do this?
__getattr__() is defined in the class to create instance attributes on
the fly. If you want class attributes you have to put the __getattr__()
method into the class of the class, or "metaclass":
class Base(object):
class __metaclass__(type):
def __getattr__(self, attr):
return lambda self, x: attr + '_' + x
def dec(callable):
return lambda *args: 'dec_' + callable(*args)
class Derived(Base):
what_so_ever = dec(Base.what_so_ever)
d = Derived()
print d.what_so_ever("42")
I don't see how you can turn this into something useful...
Peter
10x. it works. however I have another small problem. now,
d.third('blah') doesn't work because instance d doesn't have 'third'
attribute. I was expecting derived class to inherit the metaclass as
well, but it didn't.

That has nothing to do with inheritance:
>type(Derived)

<class 'classattr.__metaclass__'>

If Python doesn't find an attribute in the instance it looks it up in the
class but not in the metaclass:
>Base.third

<function <lambdaat 0x2b5f8028aa28>>>Base().third

Traceback (most recent call last):
Â* File "<stdin>", line 1, in <module>
AttributeError: 'Base' object has no attribute 'third'

I think you should go back to the drawing board now and look for a simpler
approach to solve your actual problem...

Peter

i somehow fixed the problem using:

def __getattr__(self, attr):
return
functools.partial(Base.__metaclass__.__getattr__(s elf.__class__,
attr), self)

however this looks ugly enough to look for another solution. i still
have one more question: why do I have to bind self? (without which
functions fail expecting an instance)
Sep 5 '08 #7
Alexandru Mosoi a écrit :
(snip)
i somehow fixed the problem using:

def __getattr__(self, attr):
return
functools.partial(Base.__metaclass__.__getattr__(s elf.__class__,
attr), self)

however this looks ugly enough to look for another solution. i still
have one more question: why do I have to bind self? (without which
functions fail expecting an instance)
Because the partial type doesn't implement the descriptor protocol
(which is implemented by function type to return a method).
Sep 5 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Anand | last post by:
class base: def __setattr__(self,attr,key,*unexpected): print "Base Class :",attr,key,unexpected,self.__dict__ self.__dict__ = key def __getattr__(self,attr,*unexpected): print "Base Class...
16
by: Chris | last post by:
Is there any way to make the class Z behave the same way as class Y? Chris class Y: value = 42 def __hasattr__(self, name): if name == '__int__': return True def __getattr__(self, name):
4
by: Tuure Laurinolli | last post by:
Someone pasted the original version of the following code snippet on #python today. I started investigating why the new-style class didn't work as expected, and found that at least some instances...
1
by: George Sakkis | last post by:
Searching the archives for something related to the title, I found a few relevant threads (e.g. http://tinyurl.com/avyg6 or http://tinyurl.com/b5b6v); however they don't seem to give a...
13
by: Jeremy Sanders | last post by:
Is it possible to implement some sort of "lazy" creation of objects only when the object is used, but behaving in the same way as the object? For instance: class Foo: def __init__(self, val):...
9
by: Larry Woods | last post by:
I have a method in my base class that I want ALL derived classes to use. But, I find that I can create a "Shadow" method in my derived class that "overrides" the method in my base class. Can't...
6
by: ivan.leben | last post by:
I want to write a Mesh class using half-edges. This class uses three other classes: Vertex, HalfEdge and Face. These classes should be linked properly in the process of building up the mesh by...
1
by: manstey | last post by:
Hi, I am using Python with Cache dbase, which provides pythonbind module, and intersys.pythonbind.object types. But I can't create a class based on this type: import intersys.pythonbind...
15
by: Bob Johnson | last post by:
I have a base class that must have a member variable populated by, and only by, derived classes. It appears that if I declare the variable as "internal protected" then the base class *can*...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.