Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 417 418 [419] 420 421 ... 796

Author Topic: if self.isCoder(): post() #Programming Thread  (Read 883248 times)

Orange Wizard

  • Bay Watcher
  • mou ii yo
    • View Profile
    • S M U G
Re: if self.isCoder(): post() #Programming Thread
« Reply #6270 on: September 12, 2014, 12:33:23 am »

Lisp is a thing because people haven't heard of Python.
Logged
Please don't shitpost, it lowers the quality of discourse
Hard science is like a sword, and soft science is like fear. You can use both to equally powerful results, but even if your opponent disbelieve your stabs, they will still die.

Squeegy

  • Bay Watcher
  • I don't really have any answers for you.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #6271 on: September 12, 2014, 01:18:01 am »

One thing Java does that I've never seen any other language do as well is class inheritance. And to some extent, function redundancy. Otherwise it's pretty shitty.
Logged
I think I'm an alright guy. I just wanna live until I gotta die. I know I'm not perfect, but God knows I try.
Kobold Name Generator
⚔Dueling Blades⚔
Fertile Lands
The Emerald Isles

Putnam

  • Bay Watcher
  • DAT WIZARD
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #6272 on: September 12, 2014, 01:32:26 am »

Python's class inheritance:

Code: [Select]
class Fizz:
    def __init__():
        self.fizz='fizz'
    def gimme_fizz(amount):
        if amount%3==0:
            print('fizz')
        else:
            print(amount)

class Buzz(Fizz):
    def __init__():
        self.buzz = 'buzz'
        Fizz.__init__()
    def gimme_buzz(amount):
        if amount%5==0:
            print('buzz')

a_buzzy_thing=Buzz()

a_buzzy_thing.gimme_fizz(3)

> 'fizz'

a_buzzy_thing.gimme_buzz(4)

> '4'

certainly not bad, though I didn't test this code.

Squeegy

  • Bay Watcher
  • I don't really have any answers for you.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #6273 on: September 12, 2014, 03:36:00 am »

Yeah, ok, now try putting those classes in separate files and making a buzzy thing in a third file.

e: Oh yeah and all three are in different folders.
« Last Edit: September 12, 2014, 03:37:47 am by Squeegy »
Logged
I think I'm an alright guy. I just wanna live until I gotta die. I know I'm not perfect, but God knows I try.
Kobold Name Generator
⚔Dueling Blades⚔
Fertile Lands
The Emerald Isles

Orange Wizard

  • Bay Watcher
  • mou ii yo
    • View Profile
    • S M U G
Re: if self.isCoder(): post() #Programming Thread
« Reply #6274 on: September 12, 2014, 03:53:51 am »

In your parent directory:
Code: [Select]
from folder1.fileA import Fizz
from folder2.fileB import Buzz
from folder3.fileC import Thosethingsthatyouwantedtodo
« Last Edit: September 12, 2014, 03:56:28 am by InsanityIncarnate »
Logged
Please don't shitpost, it lowers the quality of discourse
Hard science is like a sword, and soft science is like fear. You can use both to equally powerful results, but even if your opponent disbelieve your stabs, they will still die.

Squeegy

  • Bay Watcher
  • I don't really have any answers for you.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #6275 on: September 12, 2014, 04:35:02 am »

Wrong, actually:

Code: (Fizz.py) [Select]
class Fizz:
    def __init__():
        self.fizz='fizz'
    def gimme_fizz(amount):
        if amount%3==0:
            print('fizz')
        else:
            print(amount)
Code: (Buzz.py) [Select]
from folder1.Fizz import Fizz
class Buzz(Fizz):
    def __init__():
        self.buzz = 'buzz'
        super(Fizz,self).__init__()
    def gimme_fizz(amount):
        return super(Fizz,self).gimme_fizz(amount)
    def gimme_buzz(amount):
        if amount%5==0:
            print('buzz')
Code: (main.py) [Select]
from folder2.buzz import Buzz
a_buzzy_thing=Buzz()

a_buzzy_thing.gimme_fizz(3)

> 'fizz'

a_buzzy_thing.gimme_buzz(4)

> '4'
Also every folder has to have an __init__.py.

E for effort, though.

e: Looks like you don't have to explicitly define an inherited class's function and call it as a super anymore. Not sure when that got changed, you definitely had to when I first used inherited classes, or it would crash.
« Last Edit: September 12, 2014, 04:50:20 am by Squeegy »
Logged
I think I'm an alright guy. I just wanna live until I gotta die. I know I'm not perfect, but God knows I try.
Kobold Name Generator
⚔Dueling Blades⚔
Fertile Lands
The Emerald Isles

Orange Wizard

  • Bay Watcher
  • mou ii yo
    • View Profile
    • S M U G
Re: if self.isCoder(): post() #Programming Thread
« Reply #6276 on: September 12, 2014, 05:02:50 am »

Spoiler (click to show/hide)

Edit: Wait, never mind. Just tested this. I'm talking out of my arse. Ignore me.
« Last Edit: September 12, 2014, 05:15:12 am by InsanityIncarnate »
Logged
Please don't shitpost, it lowers the quality of discourse
Hard science is like a sword, and soft science is like fear. You can use both to equally powerful results, but even if your opponent disbelieve your stabs, they will still die.

Squeegy

  • Bay Watcher
  • I don't really have any answers for you.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #6277 on: September 12, 2014, 05:10:40 am »

Yes, but your example involves importing every single class held in a separate file into your main file, which is frankly just bad technique. The inheritance should be self-contained. You don't need Fizz for your main program, you only need Buzz, so you should only have to import Buzz.

Also, I'm 100% sure it's the case (I should know, I'm actively using Python). Otherwise you get this error:

ImportError: No module named Buzz

__init__.py is what lets the program know that the folder it's in is part of the program's filesystem. It will ignore any folder that doesn't have that in it.

On the plus side, you can put variables and stuff in __init__.py and call it directly, which I use for certain purposes. Since you apparently don't need to redeclare inherited functions anymore, Java loses its place as 'best at inherited classes' and is only best at redundant functions (i.e. a function with the same name that does the same thing but has multiple possible inputs).
Logged
I think I'm an alright guy. I just wanna live until I gotta die. I know I'm not perfect, but God knows I try.
Kobold Name Generator
⚔Dueling Blades⚔
Fertile Lands
The Emerald Isles

Orange Wizard

  • Bay Watcher
  • mou ii yo
    • View Profile
    • S M U G
Re: if self.isCoder(): post() #Programming Thread
« Reply #6278 on: September 12, 2014, 05:21:35 am »

Yeah, I see. I'm also actively using Python, but I have little experience with all this subdirectory malarkey.
I can say that Python can do multiple possible inputs for a function, but it's probably highly impractical. I have no idea how Java does it.
Logged
Please don't shitpost, it lowers the quality of discourse
Hard science is like a sword, and soft science is like fear. You can use both to equally powerful results, but even if your opponent disbelieve your stabs, they will still die.

Squeegy

  • Bay Watcher
  • I don't really have any answers for you.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #6279 on: September 12, 2014, 05:31:57 am »

Python does it like this:

def handleMsg(self,type,format=[''],extraTags=[],s_vars={}):
handleMsg("test")
handleMsg("test",[])
handleMsg("test",extraTags=["",""])
handleMsg("test",[''],[],{})

But it's very rigid and inflexible and only those variables can be called.

Java does it like this:

public final static double div(final double a, final double b)
{
   return a/b;
}
public final static double div(final double a, final int b)
{
   return a/((double)b);
}
public final static double div(final int a, final double b)
{
   return ((double)a)/b;
}
public final static double div(final double a, final long b)
{
   return a/((double)b);
}
public final static double div(final long a, final double b)
{
   return ((double)a)/b;
}
div(1,2.0)
div(2.0,1)
div(2.0,2.0)
div(1,1)

All of those are valid under the exact same method. It simply calls the one that uses the right variable types. You can also use it to make arguments optional, like Python does above. Sure, differentiating between doubles and ints isn't necessary in Python, but that's not the only case when having a function that does different things depending on what variables are put in might be useful; I actually think Python loses some functionality in exchange for flexibility by not assigning variables an inherent type.
« Last Edit: September 12, 2014, 05:43:37 am by Squeegy »
Logged
I think I'm an alright guy. I just wanna live until I gotta die. I know I'm not perfect, but God knows I try.
Kobold Name Generator
⚔Dueling Blades⚔
Fertile Lands
The Emerald Isles

Mephisto

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #6280 on: September 12, 2014, 06:10:23 am »

But it's very rigid and inflexible and only those variables can be called.

Method args in Python are about as non-rigid as they can get. If you include args and kwargs, you can pass random parameters to your heart's content.
Logged

Squeegy

  • Bay Watcher
  • I don't really have any answers for you.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #6281 on: September 12, 2014, 06:24:02 am »

Yes, but they're random and therefore unpredictable. Frankly, **kwargs is pretty useless unless you know what you're passing to it ahead of time, and then you need to determine which set of arguments you're using... just having multiple functions is much more elegant. **args is arguably more useful but still hardly ever used; even the Python docs say that it's rarely implemented.
Logged
I think I'm an alright guy. I just wanna live until I gotta die. I know I'm not perfect, but God knows I try.
Kobold Name Generator
⚔Dueling Blades⚔
Fertile Lands
The Emerald Isles

alexandertnt

  • Bay Watcher
  • (map 'list (lambda (post) (+ post awesome)) posts)
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #6282 on: September 12, 2014, 07:29:40 am »

Why is lisp even a thing?

Because its fun to scare OO programmers with :P
Logged
This is when I imagine the hilarity which may happen if certain things are glichy. Such as targeting your own body parts to eat.

You eat your own head
YOU HAVE BEEN STRUCK DOWN!

Orange Wizard

  • Bay Watcher
  • mou ii yo
    • View Profile
    • S M U G
Re: if self.isCoder(): post() #Programming Thread
« Reply #6283 on: September 12, 2014, 07:53:57 am »

I'm pretty sure you can get around the inflexible method arguments if you nest a few dozen "if" statements.
Logged
Please don't shitpost, it lowers the quality of discourse
Hard science is like a sword, and soft science is like fear. You can use both to equally powerful results, but even if your opponent disbelieve your stabs, they will still die.

Erils

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #6284 on: September 12, 2014, 09:02:32 am »

I'm just starting to learn Java  :D

Hopefully, I will one day actually be able to make anything other than "Hello World" work.
Logged
Pages: 1 ... 417 418 [419] 420 421 ... 796