0

Any equivalent to this one-line code in Python?

// C# just shows how class looks like
class MyClass {
    public int A {get; set;}
    public int B {get; set;}
}

// This is that one-line code in C#. Any pythonic way to do something like this?
// var x = MyClass() { A=1, B=2 } - "var" syntax sugar, at compile time it's == MyClass
MyClass x = MyClass() { A=1, B=2 }

Edit: Maybe my question wasn't so precise. My main goal is to not pass arguments to constructor.

How to initialize many class members (any combination of them) without passing them to constructor and without constructor with all default values.

Edit: Thx for answers and sorry for confusing question. I just wanted to know answer for question in topic - What is the best way (pythonic way) to initialize class's subset of attributes without explicity writing them in constructor.

7
  • 1
    Without passing them as parameters to the constructor? Commented Jul 29, 2012 at 11:20
  • 3
    Are you asking about class creation or instance creation? Commented Jul 29, 2012 at 11:22
  • 1
    your question is not clear. Be specific and tell us where you want to apply this kind of thing.. so that we can give you more appropriate answer Commented Jul 29, 2012 at 11:35
  • 2
    for k, v in {'A': 1, 'B': 2}: setattr(MyClass, k, v) Commented Jul 29, 2012 at 11:36
  • 2
    Once could say that an object/class/struct with 100 attributes, an arbitrary subset of which may have values is worse than useless. If you pass one of these objects to me, what could I reasonably expect to do with it? Qiau gave you what you asked for below, but I'm not sure you actually want what you request. Commented Jul 29, 2012 at 11:37

3 Answers 3

2

I'm not sure what you are trying to achieve but you might want to pass generic arguments to the class constructor like:

class MyClass:
    def __init__(self, **kwargs):
        for key in kwargs:
            setattr(self, key, kwargs[key])

x = MyClass(a='test', b='test2')
# x.a == 'test'
# x.b == 'test2'
y = MyClass(c=123, d='something else')
# y.c = 123
# y.d = 'something else'
Sign up to request clarification or add additional context in comments.

4 Comments

This is not what he is asking.
@Surya: In link which you posted in other comment I'm pretty sure it's the best way I can achieve what I want. I don't want to add A,B,C,D... to my constructor's parameters list, but I forgot about kwargs and setattr function. So it's the easiest way to do that. I think...
@gnibbler: Wouldn't self.__dict__.update(kwargs) be more explicit?
Using self.__dict__.update(kwargs) would bypass any attributes implemented as properties with setters; setattr does not.
2

Actually, I am not aware of C# but looking at your code, I thought this is the one that you are looking at.

def MyClass (object):
    def __init__ (A, B):
        self.A = A
        self.B = B

x = MyClass(A=1, B=2)

Edit:

If you are looking for 100 arguments, use something like **kwargs.

4 Comments

Sorry, but not really :) I just presented simple example with 2 attributes, what if there are 100 and I'd like to initialize any combination of them? I know how constructors work :) and it's not what I want. Maybe my question wasn't so clear. Edited already.
@Simon take a look at this stackoverflow.com/questions/1098549/… It should help you in that case
@Simon: It's not at all clear what you want. If you have 100's of attributes, why not just store them in a dict, instead of making them class attributes?
I've already answered for that. It was just an example. I've also edited question once more. I hope it's now clearer.
0

Well to make a class in Python just do this:

class Name:
    def __init__(self, arg1): # When an instance of a class is made this will be executed.
        self.var1 = 1
        self.var2 = 2
        self.arg1 = arg1

person1 = Name("arg1") # Create instance of the class

I personally don't know if there is a way to do this in one line pythonically as you also need to create the init() method.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.