Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 323 324 [325] 326 327 ... 796

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

Dutchling

  • Bay Watcher
  • Ridin' with Biden
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4860 on: August 30, 2013, 08:29:58 am »

In Python, what is the difference between 'is' and 'is not', and '=' and '!='? Or are they the same?
Logged

Mephisto

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4861 on: August 30, 2013, 09:06:42 am »

In Python, what is the difference between 'is' and 'is not', and '=' and '!='? Or are they the same?

I'm horrible at explanations, but it's pretty easy to explain in code.

Warning: naive code. Don't do this. Breaks if the rhs of the equality check doesn't have an attribute named 'bar'.
Code: [Select]
>>> class Foo:
...     def __init__(self, bar):
...         self.bar = bar
...     def __eq__(self, other):
...         return self.bar == other.bar
>>> x = Foo(5)
>>> y = Foo(5)
>>> x == y
True
>>> x is y
False
>>> x is not y
True
>>> y = x
>>> x is y
True

Basically, is tests that both sides refer to the same object while == tests whatever __eq__ or __cmp__ methods you've defined.
« Last Edit: August 30, 2013, 09:09:54 am by Mephisto »
Logged

Dutchling

  • Bay Watcher
  • Ridin' with Biden
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4862 on: August 30, 2013, 09:15:00 am »

So unless you use those they are the same? I tend to use 'is' and 'is not' with False, None, and True, but otherwise don't really use them.
Logged

GlyphGryph

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4863 on: August 30, 2013, 09:50:57 am »

Basically, "is" compares the internal object id, while "==" is just a regular method call that uses the defined "==" method for the object and returns the result.
Logged

alexandertnt

  • Bay Watcher
  • (map 'list (lambda (post) (+ post awesome)) posts)
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4864 on: August 30, 2013, 09:57:59 am »

What would be the fastest way to draw isometric tiles in the correct order?

Currently I am using LINQ's OrderBy, which works, but is not very fast for large numbers of tiles...
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!

Nadaka

  • Bay Watcher
    • View Profile
    • http://www.nadaka.us
Re: if self.isCoder(): post() #Programming Thread
« Reply #4865 on: August 30, 2013, 10:10:49 am »

What would be the fastest way to draw isometric tiles in the correct order?

Currently I am using LINQ's OrderBy, which works, but is not very fast for large numbers of tiles...
Why are you using an OrderBy when you could be using a 2 dimensional array?
Logged
Take me out to the black, tell them I ain't comin' back...
I don't care cause I'm still free, you can't take the sky from me...

I turned myself into a monster, to fight against the monsters of the world.

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4866 on: August 30, 2013, 11:46:16 am »

What would be the fastest way to draw isometric tiles in the correct order?

Currently I am using LINQ's OrderBy, which works, but is not very fast for large numbers of tiles...
Why are you using an OrderBy when you could be using a 2 dimensional array?
This here. Remember, view distance ordering only needs to be correct when the tiles overlap (I'm assuming you're using a grid). So you can just draw them in 2d array reading order, as long as you start with the furthest tile.

Logged

miauw62

  • Bay Watcher
  • Every time you get ahead / it's just another hit
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4867 on: August 30, 2013, 02:23:12 pm »

@Dutchling

If a is b and a = 5, b == 5 too.
If you add 3 to a, a == 8, and b == 8.

You get this situation if you define b by doing a = b.

(I don't actually remember how to assign b the value of a without causing this.)
« Last Edit: August 30, 2013, 02:29:24 pm by miauw62 »
Logged

Quote from: NW_Kohaku
they wouldn't be able to tell the difference between the raving confessions of a mass murdering cannibal from a recipe to bake a pie.
Knowing Belgium, everyone will vote for themselves out of mistrust for anyone else, and some kind of weird direct democracy coalition will need to be formed from 11 million or so individuals.

Dutchling

  • Bay Watcher
  • Ridin' with Biden
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4868 on: August 30, 2013, 03:34:27 pm »

@Dutchling

If a is b and a = 5, b == 5 too.
If you add 3 to a, a == 8, and b == 8.

You get this situation if you define b by doing a = b.

(I don't actually remember how to assign b the value of a without causing this.)
That's not necessarily right and also not something I was wondering about :P
Code: [Select]
>>> a = 5
>>> b = a
>>> a += 3
>>> b
5
But
Code: [Select]
>>> class CLASS():
True


>>> a = CLASS()
>>> b = a
>>> a.five = 5
>>> b.five
5
>>> a.five += 3
>>> b.five
8
Logged

miauw62

  • Bay Watcher
  • Every time you get ahead / it's just another hit
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4869 on: August 30, 2013, 03:41:01 pm »

Welp, fuck me :P
Logged

Quote from: NW_Kohaku
they wouldn't be able to tell the difference between the raving confessions of a mass murdering cannibal from a recipe to bake a pie.
Knowing Belgium, everyone will vote for themselves out of mistrust for anyone else, and some kind of weird direct democracy coalition will need to be formed from 11 million or so individuals.

SethCreiyd

  • Bay Watcher
  • [VESPERTINE]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4870 on: August 30, 2013, 05:01:29 pm »

In Python, the referenced value of an int is changed when you increment it.  When you have a line like ' x = 5; y = x ', what it means to the interpreter is, 'Make a new int object named x and assign it this literal value, then make a new object named y with a reference to the same value as x.'

The line ' x += 1 ' means 'Change the reference of x to a value equal to the current referenced value plus one.'

Code: [Select]
>>> a = int(255)
>>> b = 255
>>> a += 1
>>> a is b
False
>>> b += 1
>>> a is b
True

>>> # int(a) is int(b) only if (a == b <= 256).
>>> a += 1; b += 1
>>> a is b
False
>>> n = 57
>>> id(57), id(n)
(5393408, 5393408)  # same object
>>> n = 257
>>> id(257), id(n)
(38467464, 38467476)  # different objects

In the CLASS example, by assigning a.five you're changing a value referenced by both a and b.  b is not its own CLASS instance, it's a reference to the same instance that was created with 'a = CLASS()'.  b.five is a.five because b is a.

Code: [Select]
>>> a = SomeClass()
>>> a.text = "I am referenced by a."
>>> b = a
>>> b.text += " I am also referenced by b."
>>> a.text
'I am referenced by a. I am also referenced by b.'
Logged

Dutchling

  • Bay Watcher
  • Ridin' with Biden
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4871 on: August 30, 2013, 05:04:14 pm »

Which is why I use a class for my global variables/constants :V
Logged

alexandertnt

  • Bay Watcher
  • (map 'list (lambda (post) (+ post awesome)) posts)
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4872 on: August 30, 2013, 10:06:42 pm »

What would be the fastest way to draw isometric tiles in the correct order?

Currently I am using LINQ's OrderBy, which works, but is not very fast for large numbers of tiles...
Why are you using an OrderBy when you could be using a 2 dimensional array?
This here. Remember, view distance ordering only needs to be correct when the tiles overlap (I'm assuming you're using a grid). So you can just draw them in 2d array reading order, as long as you start with the furthest tile.

I forgot to mention that the world is 3d (x, y, z) and certain objects may exist between tiles.
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!

Rose

  • Bay Watcher
  • Resident Elf
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4873 on: August 30, 2013, 10:51:11 pm »

3D doesn't complicate things.  Stonesense is 3D and does the drawing pretty easily.

It's the objects between tiles that complicate things.
Logged

alexandertnt

  • Bay Watcher
  • (map 'list (lambda (post) (+ post awesome)) posts)
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4874 on: August 30, 2013, 11:18:56 pm »

3D doesn't complicate things.  Stonesense is 3D and does the drawing pretty easily.

Do you mean easily as in "easy to code" or easy as in "runs fast"?
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!
Pages: 1 ... 323 324 [325] 326 327 ... 796