473,791 Members | 3,275 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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__.u pdate(kwds)
def draw(self, x=0, y=0, scale=1.0):
# draw the object

hello = shape(text='hel lo')
redhello = hello(color='re d')
largeredhello = redhello(size=1 00)
largeredhiya = largeredhello(t ext='Hiya!')
largeredhiya.dr aw(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 1596
"Ron Adam" <rr*@ronadam.co m> 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__.u pdate(kwds)
def draw(self, x=0, y=0, scale=1.0):
# draw the object

hello = shape(text='hel lo')
redhello = hello(color='re d')
largeredhello = redhello(size=1 00)
largeredhiya = largeredhello(t ext='Hiya!')
largeredhiya.dr aw(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__.up date(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.co m> 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__.u pdate(kwds)
def draw(self, x=0, y=0, scale=1.0):
# draw the object

hello = shape(text='hel lo')
redhello = hello(color='re d')
largeredhello = redhello(size=1 00)
largeredhiya = largeredhello(t ext='Hiya!')
largeredhiya.dr aw(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__.up date(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__.u pdate(kwds)

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

return Shape(**kwds)

Kay

Oct 14 '05 #3
George Sakkis wrote:
"Ron Adam" <rr*@ronadam.co m> 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__.u pdate(kwds)
def draw(self, x=0, y=0, scale=1.0):
# draw the object

hello = shape(text='hel lo')
redhello = hello(color='re d')
largeredhello = redhello(size=1 00)
largeredhiya = largeredhello(t ext='Hiya!')
largeredhiya.dr aw(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__.up date(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(ca nvas, 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
3052
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 a single collections type, I should be proposing a new "namespaces" module instead. Some of my reasons: (1) Namespace is feeling less and less like a collection to me. Even though it's still intended as a data-only structure, the use cases...
2
3461
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 cleanly implement an inheritance mechanism. i have a hierarchy of folders in an sql table. every folder has a parentFolderID, if this value is 0 then it means it's a root folder.
6
2376
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 group. i want to use a array object in the class but i don't know how..i am just learning the c++. So i dont know how to use class. in fact, i have writen like the following: class employee { public: employee();
6
7967
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 Child object goes out of scope? Specifically, I am dealing with a C library that provides a function that must be called to "destruct" a particular struct (this struct is dynamically allocated by another provided function). To avoid memory
6
2668
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 the object and set default blank/null values), and then does all the Db access and number crunching to populate the new object. The factory returns the fully populated object to the caller. All the fields in the object are private, but have...
11
3846
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 experts. I would like to produce Javascript classes that can be "subclassed" with certain behaviors defined at subclass time. There are plenty of ways to do this through prototyping and other techniques, but these behaviors need to be static and...
15
1927
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 two? (1)
9
1923
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 the object without using the global variable ? Thank you. ///---------------The object ------------------------------- function clientENV {
8
1683
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 = ): self.ary = ary def append(self):
0
9669
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10156
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9997
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9030
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7537
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6776
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5559
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4110
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2916
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.