0

I am vrey new to python and have to work with a given code (not my implementation), with very limited accesses (no debugger, screen prints and such).

Assuming I have the following class:

class foo ():

    def __init__(self, u, v):
        #some flow

def main():
    #some flow
    x=1
    return x

main() and foo() are in the same "file.py" file. are they connected? and I instantiate it in the following way:

import file as A
MyFoo=A.main()

In oppose to:

MyFoo=foo()
  • Did I call upon the __init__ function?
  • If so how? I see no point where it is stimulated.
  • If it was indeed called (and here lies the big questions) how do I assert values for u & v?

I have tried an online compiler and still didn't manage, changing u & v values. I also read this to try and understand instantiation process in python.

havakok
  • 1,185
  • 2
  • 13
  • 45
  • So these ([1](https://docs.python.org/2/tutorial/classes.html#class-objects), [2](https://docs.python.org/2/tutorial/classes.html#class-and-instance-variables)) did not help? – DeepSpace Sep 05 '16 at 13:41
  • No because what I don understand is what happens when I call upon the `main()` method instead of a simple `foo()` call. See edit. – havakok Sep 05 '16 at 13:45
  • If `main` is supposed to be a method, then 1) it must be indented four more spaces and 2) it should take `self` as its first argument. – RemcoGerlich Sep 05 '16 at 14:00

1 Answers1

0

It would help if you stated your overall goal, but I can offer this information: __init__ is called by Python to initialize new instances of a class. If you want it called, you should say "myfoo = foo(myu,myv)". That will cause Python to invoke __new__, which will invoke __init__ for you. You should do something with u and v inside __init__, perhaps assigning them to instance attributes, like this: "self.u = u".

Here, main() is just a normal, unbound function, not an instance function or a class function of foo, not even a static function inside foo's scope, and definitely not a mainline in the C and Java sense of "mainline." If you want it to create an instance of foo, you should put "myfoo = foo(myu,myv)" inside main(project). However, the fact that you named the function "main" suggests you don't know how mainlines work in Python. See here: What does if __name__ == "__main__": do?

When you import file as A and then say "MyFoo = A.main()", all you are doing is invoking main(), which just returns 1 and does nothing with class foo. Try printing out MyFoo. You will see that it got the return value of main (which was 1), and thus MyFoo has nothing to do with class foo.

The way you are naming things suggests you are very confused. Please say your overall goal, so we can help you more.

Community
  • 1
  • 1
D-Von
  • 416
  • 2
  • 5
  • OK, got it. and as per you comments about the code. i tried to lose as many, unnecessary for the question, code as possible so I needed nothing from `u` and `v` . Sure, I have copied the actual code and kept names like main and such. the names are of no importance to me though your comment is in place. Thanks! – havakok Sep 05 '16 at 14:01
  • 1
    I edited my answer in response to your edits of your question. In particular, I emphasized that `main()` has nothing to do with class `foo`, except that it happens to be in the same file where `foo` is defined. By the way, why did you remove the base class `object` of `foo`? – D-Von Sep 05 '16 at 14:29
  • Because I don't yet understand it's functionality, and it seemed relevant to the code\question. – havakok Sep 05 '16 at 14:33
  • My main goal was to understand the `A.main()` call and it's ramifications. I was confused and you did manage to put things in order for me. I was relaying on a given "working" code that does not work. Eventually, it appeaser that an instantiation of `foo()` was indeed missing and needed in this code and it solved the problem we were having. – havakok Sep 05 '16 at 14:49
  • 1
    If you are using Py2, then inheriting from `object` makes your class a "new-style" class, which is probably what you want. If you are using Py3 and will never need to run on Py2, then whether you inherit from `object` makes no difference; there are no old-style classes in Py3. In Py2, the differences between old and new aren't that great (for instance, the \__slots__ mechanism becomes available in the new style, but that rarely matters). Still, why choose the outdated style? More: http://stackoverflow.com/questions/54867/what-is-the-difference-between-old-style-and-new-style-classes-in-python – D-Von Sep 05 '16 at 14:52