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.

Simon
  • 2,329
  • 4
  • 30
  • 49
  • 1
    Without passing them as parameters to the constructor? – Qiau Jul 29 '12 at 11:20
  • 3
    Are you asking about class creation or instance creation? – John La Rooy Jul 29 '12 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 – Surya Jul 29 '12 at 11:35
  • 2
    `for k, v in {'A': 1, 'B': 2}: setattr(MyClass, k, v)` – Joel Cornett Jul 29 '12 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. – msw Jul 29 '12 at 11:37
  • For those unfamiliar with C#, could you explain whether the value of the variable `x` is a class or an instance? – samfrances Jul 29 '12 at 11:43
  • You are right and I agree with you in 100% - that object will be useless, but an example is only an example. I just wanted to illustrate what construction I'm looking for. Maybe I wasn't perfect in my question, but I'm in rush and I've got my answer :) Thx – Simon Jul 29 '12 at 11:45

3 Answers3

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.

Surya
  • 4,824
  • 6
  • 38
  • 63
  • 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 Jul 29 '12 at 11:26
  • 2
    @Simon take a look at this http://stackoverflow.com/questions/1098549/proper-way-to-use-kwargs-in-python It should help you in that case – Surya Jul 29 '12 at 11:31
  • 2
    @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? – Joel Cornett Jul 29 '12 at 11:38
  • I've already answered for that. It was just an example. I've also edited question once more. I hope it's now clearer. – Simon Jul 29 '12 at 11:51
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'
Qiau
  • 5,976
  • 3
  • 29
  • 40
  • This is not what he is asking. – Surya Jul 29 '12 at 11:37
  • @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... – Simon Jul 29 '12 at 11:41
  • @gnibbler: Wouldn't `self.__dict__.update(kwargs)` be more explicit? – martineau Jul 29 '12 at 15:36
  • Using `self.__dict__.update(kwargs)` would bypass any attributes implemented as properties with setters; `setattr` does not. – Wil Cooley Jun 27 '16 at 18:21
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.

mattjegan
  • 2,724
  • 1
  • 26
  • 37