473,385 Members | 1,693 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,385 software developers and data experts.

object inheritance and default values


I'm trying to implement simple svg style colored complex objects in
tkinter and want to be able to inherit default values from other
previously defined objects.

I want to something roughly similar to ...

class shape(object):
def __init__(self, **kwds):
# set a bunch of general defaults here.
self.__dict__.update(kwds)
def draw(self, x=0, y=0, scale=1.0):
# draw the object

hello = shape(text='hello')
redhello = hello(color='red')
largeredhello = redhello(size=100)
largeredhiya = largeredhello(text='Hiya!')
largeredhiya.draw(c, 20, 50)
I think this will need to require __new__ or some other way to do it.
But I'm not use how to get this kind of behavior. Maybe the simplest
way is to call a method.

redhello = hello.makenew( color='red' )

But I want to be able to have all the objects access alike?

Hmmm.. I think maybe if if don't ever access shape (or Shape) directly
in my data structure, then __new__ would work? So my first default
object should be an instance of shape with a __new__ method to create
more? Ok, off to try it. But any comments will be welcome.

Cheers,
Ron
Oct 14 '05 #1
3 1574
"Ron Adam" <rr*@ronadam.com> wrote:
I'm trying to implement simple svg style colored complex objects in
tkinter and want to be able to inherit default values from other
previously defined objects.

I want to something roughly similar to ...

class shape(object):
def __init__(self, **kwds):
# set a bunch of general defaults here.
self.__dict__.update(kwds)
def draw(self, x=0, y=0, scale=1.0):
# draw the object

hello = shape(text='hello')
redhello = hello(color='red')
largeredhello = redhello(size=100)
largeredhiya = largeredhello(text='Hiya!')
largeredhiya.draw(c, 20, 50)
I think this will need to require __new__ or some other way to do it.
But I'm not use how to get this kind of behavior. Maybe the simplest
way is to call a method.

redhello = hello.makenew( color='red' )


Just name it '__call__' instead of makenew and you have the syntax sugar you want:

def __call__(self, **kwds):
new = self.__class__(**self.__dict__)
new.__dict__.update(kwds)
return new

Personally I would prefer an explicit method name, e.g. 'copy'; hiding the fact that 'shape' is a
class while the rest are instances is likely to cause more trouble than it's worth.

George
Oct 14 '05 #2

George Sakkis wrote:
"Ron Adam" <rr*@ronadam.com> wrote:
I'm trying to implement simple svg style colored complex objects in
tkinter and want to be able to inherit default values from other
previously defined objects.

I want to something roughly similar to ...

class shape(object):
def __init__(self, **kwds):
# set a bunch of general defaults here.
self.__dict__.update(kwds)
def draw(self, x=0, y=0, scale=1.0):
# draw the object

hello = shape(text='hello')
redhello = hello(color='red')
largeredhello = redhello(size=100)
largeredhiya = largeredhello(text='Hiya!')
largeredhiya.draw(c, 20, 50)
I think this will need to require __new__ or some other way to do it.
But I'm not use how to get this kind of behavior. Maybe the simplest
way is to call a method.

redhello = hello.makenew( color='red' )


Just name it '__call__' instead of makenew and you have the syntax sugar you want:

def __call__(self, **kwds):
new = self.__class__(**self.__dict__)
new.__dict__.update(kwds)
return new

Personally I would prefer an explicit method name, e.g. 'copy'; hiding the fact that 'shape' is a
class while the rest are instances is likely to cause more trouble than it's worth.

George


Symmetry can be achieved by making shape a factory function of Shape
objects while those Shape objects are factory functions of other Shape
objects by means of __call__:

def shape(**kwds):
class Shape(object):
def __init__(self,**kwds):
self.__dict__.update(kwds)

def __call__(self, **kwds):
new = self.__class__(**self.__dict__)
new.__dict__.update(kwds)
return new

return Shape(**kwds)

Kay

Oct 14 '05 #3
George Sakkis wrote:
"Ron Adam" <rr*@ronadam.com> wrote:

I'm trying to implement simple svg style colored complex objects in
tkinter and want to be able to inherit default values from other
previously defined objects.

I want to something roughly similar to ...

class shape(object):
def __init__(self, **kwds):
# set a bunch of general defaults here.
self.__dict__.update(kwds)
def draw(self, x=0, y=0, scale=1.0):
# draw the object

hello = shape(text='hello')
redhello = hello(color='red')
largeredhello = redhello(size=100)
largeredhiya = largeredhello(text='Hiya!')
largeredhiya.draw(c, 20, 50)
I think this will need to require __new__ or some other way to do it.
But I'm not use how to get this kind of behavior. Maybe the simplest
way is to call a method.

redhello = hello.makenew( color='red' )

Just name it '__call__' instead of makenew and you have the syntax sugar you want:

def __call__(self, **kwds):
new = self.__class__(**self.__dict__)
new.__dict__.update(kwds)
return new

Personally I would prefer an explicit method name, e.g. 'copy'; hiding the fact that 'shape' is a
class while the rest are instances is likely to cause more trouble than it's worth.

George


Just got it to work with __call__ as a matter of fact. ;-)

def __call__(self,**kwds):
for key in self.__dict__:
if key not in kwds:
kwds[key] = self.__dict__[key]
return shape(**kwds)

The purpose having the objects not call the methods explicityly in this
case is to simplify the data structure in a way that it doesn't care.
The point is to create as much consistancy in the data structure as
possible without having to special case some objects as base objects,
and some as instances.

# Triangle
triangle = shape( obj='regpolygon',
points=getrpoly(3),
fill='grey',
size=75
)

# Text
text = shape( obj='text', fill='black', size=10 )

# CAUTION ICON
caution = group( triangle(x=6, y=5),
triangle(fill='yellow'),
text( text='!',
x=39, y=32, size=35,
font='times', style='bold' )
)

I can use a shape() in the group exactly like triangle(), or text().
They are all the same thing to group. It's just a matter of what the
defaults are. This keeps things very simple. ;-)

Then when it needs to be drawn...

caution.draw(canvas, x, y, scale)

I still need to work on reusing and nesting groups and having them set
default values. Maybe I need to make group a sub shape which contains a
list of shapes, etc...

This is another work it out as I go project. ;-)

Cheers,
Ron







Oct 14 '05 #4

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

Similar topics

18
by: Steven Bethard | last post by:
In the "empty classes as c structs?" thread, we've been talking in some detail about my proposed "generic objects" PEP. Based on a number of suggestions, I'm thinking more and more that instead of...
2
by: Tim Mackey | last post by:
hi folks, i'm puzzled over this one, anyone with some solid db experience might be able to enlighten me here. i'm modelling a file system in a database as follows, and i can't figure out to...
6
by: surrealtrauma | last post by:
i have a trouble about that: i want to ask user to enter the employee data (employee no., name, worked hour, etc.), but i dont know how to sort the data related to a particular employee as a...
6
by: Squeamz | last post by:
Hello, Say I create a class ("Child") that inherits from another class ("Parent"). Parent's destructor is not virtual. Is there a way I can prevent Parent's destructor from being called when a...
6
by: BBM | last post by:
I have an object that has a fairly complex construction sequence, so I have written a dedicated "factory" class that invokes the constructor of my object class (which does nothing but instantiate...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
15
by: Sam Kong | last post by:
Hello! I got recently intrigued with JavaScript's prototype-based object-orientation. However, I still don't understand the mechanism clearly. What's the difference between the following...
9
by: Cylix | last post by:
The following example are going to create an object to store the client Information, I would like to new the object to init all the properties by function: setProperty() Can I set the value to...
8
by: matthewperpick | last post by:
Check out this toy example that demonstrates some "strange" behaviour with keyword arguments and inheritance. ================================= class Parent: def __init__(self, ary = ):...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.