Extending class that is in another file

The more I use python the more I dislike it. It seems really great at the start. But so many things are so shit.

Anyway, this works great:

class Foo:
    def __init__(self):
        global BAR
        BAR = -1


class Bananas(Foo):

    def __init__(self):
        Foo.__init__(self)
        print str(BAR)


if __name__ == "__main__":
    b = Bananas()       

But how can I do the same, but having Foo in it's own file?

Answers

  • Here is a slight varation:

    my_lib.py:

    class Foo:
    
        BAR = -1
    
        def __init__(self):
            pass
    

    main.py:

    from my_lib import Foo
    
    
    class Bananas(Foo):
    
        def __init__(self):
            Foo.__init__(self)
            print str(Foo.BAR)
    
    
    if __name__ == "__main__":
        b = Bananas()   
    

    But I really don't want to type Foo.BAR but just type BAR! Whatever I try, there is always breaking one things.

  • edited May 2017 Answer ✓

    ... but just type BAR!

    • What you're asking for is ordinarily used a lot in languages such as Java & C#. ~O)
    • However it's against what is known as the Zen of Python's principles: [-X
      https://en.Wikipedia.org/wiki/Zen_of_Python
    • "Explicit is better than implicit." is the 2nd principle over there btW. ;;)
    • BAR is a member of class Foo.
    • Therefore you've gotta prefix it w/ Foo. in order to affirm it belongs to that class, and it's not a mere variable or something else. :-\"
    • Prefixing w/ parameter self is an alternative to Foo.BAR too: self.BAR
    • Anyways, I've already showed ya a hack on how to make sure an imported global variable doesn't lose its original object's reference when using operators w/ = in your previous post: L-)

    https://forum.Processing.org/two/discussion/22610/create-a-library-that-can-be-used-global#Item_2

  • This conflicts with the first one:

    Beautiful is better than ugly. I will just use the ugly prefixes then.

  • edited May 2017

    Just to clarify: I do not follow Python's "zenness"! >:)
    I very much prefer cleverness over puritanism. :ar!
    And I love we don't need to prefix everything w/ this. in Java! ~O)

  • Also I really love curly braces!

  • Hmm... I don't care about curly braces and semicolons. :>

  • @GoToLoop How do ppl keep track of function scopes without curly brackets? Even if the IDE does some job for you, if you extends beyond the number of line your display can output, how would you match scopes? Are you tie to use specific IDEs? I wonder how would emacs handle python coding...

    Kf

  • edited May 2017

    Via indentations! Even simple text editors like NotePad2 keeps the current indentation after an ENTER key press. =P~

  • Hmmm if you have multiple nested if statements... how do you keep track and ensures you are in the right scope? I should probly check it out but I am not keen in working in python at the moment. Having no curly brackets just make my eyes spin 8-|

    Kf

  • edited May 2017

    ... if you have multiple nested if statements...

    Multiple nested branches are bad.
    Big sections can be replaced by function calls though.
    For really long 1s, switch () / case: is another option.
    Although I guess switch () / case: doesn't exist in Python. 3:-O

  • Having no curly brackets just make my eyes spin

    "I dunno ... whitespace?"

Sign In or Register to comment.