|
Thread Rules 1. This is not a "do my homework for me" thread. If you have specific questions, ask, but don't post an assignment or homework problem and expect an exact solution. 2. No recruiting for your cockamamie projects (you won't replace facebook with 3 dudes you found on the internet and $20) 3. If you can't articulate why a language is bad, don't start slinging shit about it. Just remember that nothing is worse than making CSS IE6 compatible. 4. Use [code] tags to format code blocks. |
|
Now I want to ask about classes in Python. The tutorial I was reading didn't even touch them! So i thought maybe they don't exist? But then how does inheritance work?
So I looked it up and they do exist? But it seems that unlike java, they are not a requirement.
so, separate .py files are "modules", which you can import and use in other python files?
It seems like modules in python are a lot like upper level classes in java.
But classes in python are also similar to classes in java in that you instantiate them? Is that the actual purpose of classes in python? Object instantiation? (or I guess more specifically, defining new types of objects for instantiation)
Also in python if I define a variable at the class level, this variable isn't global, right? It needs to be at the level above that?
|
Classes work almost exactly the same as in Java. You're just not forced to use them. And yes, you can have multiple classes together in a single file (module). Modules are nothing like classes. They are more like Java packages, but with some extra functionality (namely, they can contain code that isn't in classes), but most importantly, you don't inherit jack shit from modules.
Scoping in Python is pretty simple and described in the manual, but here's a SO answer that lifts the right part from the manual: http://stackoverflow.com/questions/291978/short-description-of-scoping-rules
|
I am so confused about global variables. I am told this should be intuitive but I don't understand how to do something.
I want a global variable, let's call it myWord.
I want a function that modifies myWord. let's call it myFunction
so my python file could look like
myWord = ""
def myFunction(): global myWord myWord = myWord + "more words"
this is basically what I am trying to do. But python doesn't like this. it tells me I need to define(initialize?) the global variable within the function
But that's what I don't understand! if I do that, if I say myWord = "" in my function, then what I am trying to do will be reset every time I run the function!
Also why the heck would I need to initialize it in there anyways, isn't the entire point that it's a global variable and it was created at the global level?
|
On January 11 2017 00:52 travis wrote:I am so confused about global variables. I am told this should be intuitive but I don't understand how to do something. I want a global variable, let's call it myWord. I want a function that modifies myWord. let's call it myFunction so my python file could look like myWord = ""
def myFunction(): global myWord myWord = myWord + "more words"
this is basically what I am trying to do. But python doesn't like this. it tells me I need to define(initialize?) the global variable within the function But that's what I don't understand! if I do that, if I say myWord = "" in my function, then what I am trying to do will be reset every time I run the function! Also why the heck would I need to initialize it in there anyways, isn't the entire point that it's a global variable and it was created at the global level? Works just fine for me... you sure you don't have some other code cluttering up your file?
|
On January 11 2017 01:34 Acrofales wrote:Show nested quote +On January 11 2017 00:52 travis wrote:I am so confused about global variables. I am told this should be intuitive but I don't understand how to do something. I want a global variable, let's call it myWord. I want a function that modifies myWord. let's call it myFunction so my python file could look like myWord = ""
def myFunction(): global myWord myWord = myWord + "more words"
this is basically what I am trying to do. But python doesn't like this. it tells me I need to define(initialize?) the global variable within the function But that's what I don't understand! if I do that, if I say myWord = "" in my function, then what I am trying to do will be reset every time I run the function! Also why the heck would I need to initialize it in there anyways, isn't the entire point that it's a global variable and it was created at the global level? Works just fine for me... you sure you don't have some other code cluttering up your file?
oh wow really?
well here is the actual code (im still playing that code challenge solving game, it's pretty fun)
def recall_password(cipher_grille, ciphered_password): result = "" def doIt(temp): global result j = 0 for w in temp: j += 1 i = 0 for c in w: i += 1 if c == "X": thisString = ciphered_password[j-1] result = result + thisString[i-1] doIt(cipher_grille)
here is the error it gives
NameError: name 'result' is not defined doIt, 18 recall_password, 20
line 18 is: result = result + thisString[i-1]
|
+ Show Spoiler +On January 11 2017 00:52 travis wrote:I am so confused about global variables. I am told this should be intuitive but I don't understand how to do something. I want a global variable, let's call it myWord. I want a function that modifies myWord. let's call it myFunction so my python file could look like myWord = ""
def myFunction(): global myWord myWord = myWord + "more words"
this is basically what I am trying to do. But python doesn't like this. it tells me I need to define(initialize?) the global variable within the function But that's what I don't understand! if I do that, if I say myWord = "" in my function, then what I am trying to do will be reset every time I run the function! Also why the heck would I need to initialize it in there anyways, isn't the entire point that it's a global variable and it was created at the global level?
did you jump to python from java :D what about not using global variables? more than often they are an antipattern.
|
result isn't global. It's in the enclosing scope. And to be more specific, your global variable result is defined when you say "global result", but you haven't initialized it anywhere.
|
Yeah. Thats what i get:
myWord = ""
def myFunction(): global myWord myWord = myWord + "more words"
myFunction()
print(myWord)
as expected it prints more words to terminal
And as pointed out in Your code "result" isnt global. It is local to "def recall_password" function.
|
ohhhhhhh right, god im dumb
thanks guys
i forgot that the entire block of code was put in the website's function (def recall_password(cipher_grille, ciphered_password)
On January 11 2017 01:45 mantequilla wrote:+ Show Spoiler +On January 11 2017 00:52 travis wrote:I am so confused about global variables. I am told this should be intuitive but I don't understand how to do something. I want a global variable, let's call it myWord. I want a function that modifies myWord. let's call it myFunction so my python file could look like myWord = ""
def myFunction(): global myWord myWord = myWord + "more words"
this is basically what I am trying to do. But python doesn't like this. it tells me I need to define(initialize?) the global variable within the function But that's what I don't understand! if I do that, if I say myWord = "" in my function, then what I am trying to do will be reset every time I run the function! Also why the heck would I need to initialize it in there anyways, isn't the entire point that it's a global variable and it was created at the global level? did you jump to python from java :D what about not using global variables? more than often they are an antipattern.
I don't know how to design my program in a way where I don't have to use global variables
|
Return more things is one quick and easy way generally(which in Python is pretty easy).
|
On January 11 2017 02:09 phar wrote: Return more things is one quick and easy way generally(which in Python is pretty easy).
Yeah, the ability to return more than one thing from a Python function has always amazed and baffled me at the same time.
|
On January 11 2017 04:19 Manit0u wrote:Show nested quote +On January 11 2017 02:09 phar wrote: Return more things is one quick and easy way generally(which in Python is pretty easy). Yeah, the ability to return more than one thing from a Python function has always amazed and baffled me at the same time. How?
|
apparently it's one of the many syntactic sugars of python
>>> def foo(): ... return 1, 2, 3, 4 # Returns a tuple >>> foo() (1, 2, 3, 4)
>>> a, b, c, d = foo() >>> a 1 >>> b 2 >>> c 3 >>> d 4
not my cup of tea though. Better return a well defined object
|
I meant: How is that amazing and baffling? Returning one object is just as good as returning many. If worst comes to worst return an array of objects. Not a clean solution by any definition but 100% functional.
|
Since i know only python i have to ask. Which langauges limit function to returning one item?
|
On January 11 2017 17:06 Silvanel wrote: Since i know only python i have to ask. Which langauges limit function to returning one item? Most popular languages only have single return values, as far as I know. That single value still can be a tuple or (reference/pointer to) anything you want though.
|
On January 11 2017 17:06 Silvanel wrote: Since i know only python i have to ask. Which langauges limit function to returning one item?
This whole discussion is confused because without defining what an "item" is it's meaningless.
>>> def test(): ... return 1,2 ... >>> test() (1, 2) >>> type(_) <class 'tuple'>
You're still returning one item, the item just happens to be a container. Python has some syntactic sugar around creating tuples and destructing tuples, such that if you have a collection of N items on rhs of assignment, you can assign it to [x0..xN) variables on the left hand side. Other languages have more advanced pattern matching, e.g. any trivial haskell example that matches (x:xs) on a list.
You can work around the lack of tuples in Java with a tuple object, but you can't do the variable destructing on the return value such that you assign multiple variables to the items of your collection in one line.
Go is a language where you can really return multiple values as a special quirk of the language, where you're not returning a tuple. They trade off making multiple returns a special case for saving complexity of implementing tuples + maybe language designers just don't like them.
|
I have a bit of a problem with the DB:
A: has one B belongs to C
B: belongs to A has many C
C: belongs to B
How do I set my cascades properly so that when I delete A all of B and C are also removed? Right now I'm getting an error that C can't be deleted because it's still referenced by A...
|
On January 11 2017 18:46 Manit0u wrote:I have a bit of a problem with the DB: A: has one B belongs to C
B: belongs to A has many C
C: belongs to B
How do I set my cascades properly so that when I delete A all of B and C are also removed? Right now I'm getting an error that C can't be deleted because it's still referenced by A... Looks like you created a deadlock situation. Change the restrictions on your DB, or accept that there will be undeletable entries. Your only other option is a hack where you add a C called "tobedeleted" or something, and change A to belong to that first, then cascade your delete.
|
|
|
|