472,378 Members | 1,219 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,378 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 849
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...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.
0
by: F22F35 | last post by:
I am a newbie to Access (most programming for that matter). I need help in creating an Access database that keeps the history of each user in a database. For example, a user might have lesson 1 sent...

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.