Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Python classes?

Name: Anonymous 2011-02-12 21:56

Let's say I have the following code in Python:


class dong():
   x = None
   def __init__(self):
      pass
   def dat_func(self):
      print x.upper()


We know that Python is dynamically typed. So I can change the type of the class member x at run-time when ever I want. That means that for every method in a python class where you have code that depends on the type of the class member x you have to check the integrity of x such that the type of x is what you want it to be.

This code demonstrates the problem.
[code]
x = dong()
x.x = 10
x.dat_func() #error, dat_func expects a str object.

For every fucking class member, any time you want to use it . . . You have to check the type to remind retarded developers that they're wrong.

Now if Python had private members it would be easier, but it fucking doesn't.

Prog 1, Python 0.

Name: Anonymous 2011-02-12 22:00

What if there is a thread that changes the type of x after you check the integrity of x but before you use it :\

Name: Anonymous 2011-02-12 22:07

>>2
That would be a failure to consider safe threading (the programmer is to blame most likely) and Python would only be at fault for doing what it was asked to do.

Name: Anonymous 2011-02-12 22:40

This is what Java programmers actually believe!
Not >>1, though, I don't hope. Though it was nice of you not to play the dynamic/weak typing confusion card.
I shall respond in turn: You should practically never be checking the type before you use something; duck typing and EAFP are tenets of Python programming. Your example is already going to fail, noisily and obviously. An type check isn't going to do shit for you or anyone else.
If you really needed the control, e.g. because an easy mistake to make could cause silent data corruption (you bad designer, you!), you should use a property and check the type at assignment.

Name: Anonymous 2011-02-12 22:57

>>4
OP here, am a faggot. Please explain duck typing and EAFP.

Name: Anonymous 2011-02-12 23:07

That piece of code won't work even if dong.x is of the correct type, in dat_func you're referring to x like it was a global variable, which it of course isn't.

Name: Anonymous 2011-02-12 23:26

>>6
Yes, OP forgot the self but that's not the point here.

Name: Anonymous 2011-02-12 23:27

>>5
I shall refer to the Python documentation, that explains it at least as well as I would:
http://docs.python.org/py3k/glossary.html#term-duck-typing

Simply put, you shouldn't need to check the type. If it has all necessary properties (even if it's not exactly the type you expected it to be), everything will work. If not, you will get an error when the code tries to use it anyway.

Name: Anonymous 2011-02-12 23:30

Do I need to have autism to be a good coder?

Name: Anonymous 2011-02-12 23:41

>>9
gtfo vipper

Name: Anonymous 2011-02-12 23:41

>>9
Let's not have another fucking thread about autism. Enough with the autism. It's a /b/ fad or something. It's nothing to do with us.

Name: Anonymous 2011-02-12 23:58

>>11
Do you have autism?

Name: Anonymous 2011-02-13 0:05

>>7

It shouldn't be self either, x is a static variable of dong, so you should refer to it as dong.x. The reason you could use self here is because you defined it earlier with the x.x = stmt statement.

Name: Anonymous 2011-02-13 3:19

is this thread over yet?
you're talking about scope and forgetting yourself

Name: VIPPER 2011-02-13 3:42

>>10
Its spelled VIPPER and you seem like an imageboardtard, please go away.
>>9
Ofcourse
>>11
Now you should know by now that World4ch is filled with autists, that is the norm here.

Name: Anonymous 2011-02-13 7:02

>>15
I said GET THE FUCK OUT, YOU FUCKING ``VIPPER''.

Name: VIPPER 2011-02-13 7:05

>>16
Or what?

Name: Anonymous 2011-02-13 7:08

>>17
Or else I shall conjure the spirits of the computer upon you with my spells, infidel!

Name: VIPPER 2011-02-13 7:13

>>18
No Anonymous, you are the infidels.

Name: Anonymous 2011-02-13 7:17

And then VIPPER was a JEW.

Name: Anonymous 2011-02-13 8:35

I started the VIPPER hating.

Name: Anonymous 2011-02-13 12:35

<--- check my dubs

Name: Anonymous 2011-02-13 18:40

thisThread = None
i simply don't understand why OP would try to call toUpper() on anything that's not expected to be a string.


#!/usr/bin/python

class dClass():
    x = None
    def __init__(self):
        pass
    def foo(self,x):
        return str(x).upper()
    def bar(self):
        try: return self.x.upper()
        except: print "no"
if __name__=="__main__":
    dong = dClass()
    dong.foo(9)
    dong.x=82384
    print dong.bar()
    dong.x="lowercase"
    print dong.bar()
    print dong.x


so what's the problem?

Name: Anonymous 2011-02-13 18:46

>>23
The indentation. And the non-private privates.

Name: Anonymous 2011-02-13 22:05

’‘AmIdoingitrightguize?“” lol vipper…
25 get
haha dubsz
Python is teh gay

Name: Anonymous 2011-02-13 22:49

>>24
it was a proof of concept on how OP's post didn't make sense, it's not meant to be functional.
>>25
nice quotes


NO SIDE EFFECTS___NO EXCEPTIONS

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List