473,403 Members | 2,293 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,403 software developers and data experts.

[CONSTRUCT] - Removing Need For Repeated Definition of __str__

I have some python code which looks similar to this:

class Car(BaseClass) :
manufacturer = factory.string()
model = factory.string()
modelYear = factory.integer()

def __str__(self):
return '%s %s %s' % (self.modelYear, self.manufacturer,
self.model)

def factory.string(self)
s = String() # creates a string object
#... # does several things
return s # returns the string object

-

I would like to simplify it in this way:

class Car(BaseClass):
manufacturer = factory.string(2) # 2 = position number...
model = factory.string(3) # ...withinn __str__
modelYear = factory.integer(1)

def factory.string(self, position)
s = String() # creates a string object
... # does several things
# creates somehow the __str__ functionality...

return s # returns the string object

-

How could I achieve this?

..

--
http://lazaridis.com
Apr 3 '06 #1
1 1236
Ilias Lazaridis wrote:
I have some python code which looks similar to this:

class Car(BaseClass) :
manufacturer = factory.string()
model = factory.string()
modelYear = factory.integer()

def __str__(self):
return '%s %s %s' % (self.modelYear, self.manufacturer,
self.model)

def factory.string(self)
s = String() # creates a string object
#... # does several things
return s # returns the string object

-

I would like to simplify it in this way:

class Car(BaseClass):
manufacturer = factory.string(2) # 2 = position number...
model = factory.string(3) # ...withinn __str__
modelYear = factory.integer(1)

def factory.string(self, position)
s = String() # creates a string object
... # does several things
# creates somehow the __str__ functionality...
return s # returns the string object

-

How could I achieve this?

.


I'm slightly confused about your use of factory functions for making
instance variables (perhaps you could explain that?). Without knowing
more about that, here's a mixin solution I've used in the past (note
that __strdef__ is something I just made up):

class SmartStr(object):

def __str__(self):
return "<%s %s>" % (self.__class__.__name__,
", ".join(attrname + "=" + str(getattr(self, attrname))
for attrname in self.__strdef__))

class Car(SmartStr):

__strdef__ = ["model_year", "manufacturer", "model"]

def __init__(self, manufacturer, model, model_year):
self.manufacturer = manufacturer
self.model = model
self.model_year = model_year

c = Car("Toyota", "Camry", 1990)
print c # => <Car model_year=1990, manufacturer=Toyota, model=Camry>

Apr 3 '06 #2

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

Similar topics

15
by: Jim Newton | last post by:
hi all, does anyone know what print does if there is no __str__ method? i'm trying ot override the __repr__. If anyone can give me some advice it would be great to have. I have defined a...
3
by: Neil Zanella | last post by:
Hello, In Python, classes are objects. But there is no way to custom print a class object. This would require some syntax such as the one commented out below: With the current "foo =...
12
by: Ole Noerskov | last post by:
The function below is supposed to remove all childnodes with a name that starts with "keywords" in "myform" in the window.opener. It works fine if there's one keyword node. But you have to run...
1
by: Edward C. Jones | last post by:
#! /usr/bin/env python class A(list): def __init__(self, alist, n): list.__init__(self, alist) self.n = n def __str__(self): return 'AS(%s, %i)' % (list.__str__(self), self.n)
16
by: Ilias Lazaridis | last post by:
I have a few small questions subjecting python functionality, most importantly the "alias_method". - *IMPORT* I would like to know, if this construct is valid, or if it can result in...
6
by: Alien | last post by:
According to MSDN, removing elements invalidates only those iterators that had specifically pointed at the removed elements. But I tried following codes, and find that without the repeated...
5
by: Mike Krell | last post by:
I'm running into problems trying to override __str__ on the path class from Jason Orendorff's path module (http://www.jorendorff.com/articles/python/path/src/path.py). My first attempt to do...
5
by: Konstantinos Pachopoulos | last post by:
Hi, i have the following class: =========================================== class CmterIDCmts: def __init__(self,commiterID,commits): self.commiterID_=long(commiterID)...
1
by: Nikhil.S.Ketkar | last post by:
Hi, Does the following construct qualify as overloading on return type ? If not, in what way ? I overloaded the type conversion operator and used pointers to member functions. Its pretty...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
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
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,...

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.