newbe questions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Daniel Schüle

    newbe questions

    Hi all

    i am new to python but not to programming in general
    i tryed the code below i would write in C++
    in particular the nested classes and creation of
    objects in constr

    my questions are
    why are the variables x_ and y_ not set properly?
    constr of base_inner never runs?!
    but the initialisation of z_ trough foo() seems to work!!!


    is the (let's call it) class-namespace resolution
    trough self the right way to do it? (for types as well as for variables)
    see self.y_ = self.base_inner ()
    ^^ ^^
    variable type

    i think x_ doesnt need self.base_inner () for the construction
    because on its place the definition of class base_inner
    is visible for it


    thanks for your time

    ps: is there any tutorials how to learn python coming from C++ background
    especially what are the pitfalls .. etc


    *************** *************** *************** **********
    import os

    os.system("cls" )

    def foo():
    return 1

    class base:
    def __init__(self):
    print "base"
    self.y_ = self.base_inner ()
    def show(self):
    print self.x_.value_
    print self.y_.value_
    print self.z_
    class base_inner:
    def __init_(self):
    print "base::x"
    self.value = "ok"
    value_ = "not ok"
    x_ = base_inner()
    y_ = None
    z_ = foo()

    class derived(base):
    def __init__(self):
    print "derived"

    b = base()
    #d = derived()

    b.show()
    #d.show()


  • Diez B. Roggisch

    #2
    Re: newbe questions

    Hi,
    [color=blue]
    > class base:
    > def __init__(self):
    > print "base"
    > self.y_ = self.base_inner ()
    > def show(self):
    > print self.x_.value_
    > print self.y_.value_
    > print self.z_
    > class base_inner:
    > def __init_(self):[/color]

    your __init__ method lacks a second underscore. BTW: Why do you use inner
    classes at all? The make sense in e.g. java, because they can overcome some
    limitations in java itself. The only thing I ever used them in c++ was to
    create a sort of namespace, which is also not neccessary because of
    modules. What are your reasons for using them?

    Regards,

    Diez

    Comment

    • Daniel Schüle

      #3
      Re: newbe questions

      [..]
      [color=blue]
      > your __init__ method lacks a second underscore. BTW: Why do you use inner
      > classes at all? The make sense in e.g. java, because they can overcome[/color]
      some[color=blue]
      > limitations in java itself. The only thing I ever used them in c++ was to
      > create a sort of namespace, which is also not neccessary because of
      > modules. What are your reasons for using them?[/color]

      thank you for your quick answear

      i dont really need them, i was just playing with python
      and possibilities of this language

      in C++ i use them to emphasis the logical relationsship of some classes to
      each other
      for example
      class list{/**/class node {/**/} };
      class tree{/**/class node {/**/} };
      or for the iterator implementation

      --
      Daniel


      Comment

      Working...