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

create instance attributes for every method argument

I remember reading somewhere how to create an instance attribute for
every method argument, but although Google is my friend, I can't seem
to find it. This could likely be done way more elegant:

=========================
class Test(object):

def __init__(self, a, b, c, d, e, f):
self.a = a
self.b = b
self.c = c
self.d = d
=========================

2B
Jul 19 '08 #1
3 877
Berco Beute wrote:
I remember reading somewhere how to create an instance attribute for
every method argument, but although Google is my friend, I can't seem
to find it. This could likely be done way more elegant:

=========================
class Test(object):

def __init__(self, a, b, c, d, e, f):
self.a = a
self.b = b
self.c = c
self.d = d
=========================

2B
IMHO you can't do much better than that with positional arguments, but you can
if they are keyword arguments.

class foo(object):
def __init__(self, **kwargs):
self.__dict__.update(kwargs)

-Larry

Jul 19 '08 #2
Berco Beute <cy*****@gmail.comwrote:
I remember reading somewhere how to create an instance attribute for
every method argument, but although Google is my friend, I can't seem
to find it. This could likely be done way more elegant:

=========================
class Test(object):

def __init__(self, a, b, c, d, e, f):
self.a = a
self.b = b
self.c = c
self.d = d
=========================

2B
You *could* do something like this:
>>class Test(object):
def __init__(self, a, b, c, d, e, f):
self.update(locals())

def update(self, adict):
for k in adict:
if k != 'self':
setattr(self, k, adict[k])

>>c = Test(1, 2, 3, 4, 5, 6)
c.__dict__
{'a': 1, 'c': 3, 'b': 2, 'e': 5, 'd': 4, 'f': 6}
>>>
but to be honest, your original is much clearer as it expresses the
intention without any obfuscation and as soon as you want to do anything
more than simply copying all arguments you'll want to do the assignments
individually anyway.
Jul 19 '08 #3
Berco Beute wrote:
I remember reading somewhere how to create an instance attribute for
every method argument, but although Google is my friend, I can't seem
to find it. This could likely be done way more elegant:

=========================
class Test(object):

def __init__(self, a, b, c, d, e, f):
self.a = a
self.b = b
self.c = c
self.d = d
=========================
http://code.activestate.com/recipes/280381/

Personally, I prefer to spell it out like you did above.

Peter
Jul 19 '08 #4

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

Similar topics

26
by: Fernando M. | last post by:
Hi, i was just wondering about the need to put "self" as the first parameter in every method a class has because, if it's always needed, why the obligation to write it? couldn't it be implicit?...
1
by: MKoleoso | last post by:
Problem: C#- Unable to create instance of a class implementing from an interface I have: namespace someNamespace { public __gc class SomeClass1 { }
39
by: Quick Fox | last post by:
Hi All, Please help for following case: How to Load a Assembly from DLL file and create instance of the class in the loaded file. I want make a function that get 2 string parameters...
5
by: mlev | last post by:
I want function in one namespace to create instance of class defined in another namespace. (more detailed - I try to make independent "Seralizer" dll, to be used by different application, each...
10
by: John M. Gabriele | last post by:
The following short program fails: ----------------------- code ------------------------ #!/usr/bin/python class Parent( object ): def __init__( self ): self.x = 9 print "Inside...
4
by: kplkumar | last post by:
This is what my supervisor is looking for. We want runtime debugging/trace logging. In other words, we want to log every method that was executed, perhaps the variables in it as well - everytime...
6
by: Rob Cowie | last post by:
Hi all, Is there a simple way to call every method of an object from its __init__()? For example, given the following class, what would I replace the comment line in __init__() with to result...
3
by: Joseph Lu | last post by:
Hi, all Could any boday tell me why I failed to create instance here in the following code ? Codes sample -------------------------------------------------------------------- void...
2
by: Jake K | last post by:
I have about 100 methods in an application I'm developing. There will be many, many more methods added as the application grows. I need to include a line of code at the start of every method in...
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
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: 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
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...
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...

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.