Python basic class understanding [closed] - python

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
So I'm trying to understand the basics of Classes and objects. I have small example that I'm trying to figure out but I am not fully understanding it. Here is the example:
You need to create a simple class such that a call to the meow() function of the class Cat(as in the example below):
kitty = Cat(3)
kitty.meow()
prints "I have 3 lives" to standard output.
This is what I have so far.
class Cat(?):
def __init__(self, kitty):
self.kitty = kitty
def meow(self):
??
kitty = Cat(3) kitty.meow()

class Cat:
def __init__(self, lives):
self.lives = lives
def meow(self):
print "I have " + str(self.lives) + " lives"
kitty = Cat(3)
kitty.meow()

Related

Something I don't understand in classes [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 days ago.
Improve this question
I am new and there is some things I don't understand in classes when I am using python .
-The self whit the __init__ method :
I don't really understand what is the meaning of the self when I am defining this
-Methods :
Let's say we have this class :
class Car():
def __init__(self, make, year)
self.make = make
etc.
How can I be referring to something that doesn't even exist
-Functions :
Why do I always have to put self as a parameter in a function
-Another class :
Let's say we have this class :
class Student:
student1 = Student()
student1.name = 'harry'
how could I put the name method if it doesn't even exist too
I am a little bit confused , thank you for answering
I try to watch some YouTube videos but I still can't figure it out

Python - How to access Instance variables? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
This post was edited and submitted for review 23 days ago.
Improve this question
I recently stumbled upon a problem, How do i access a class's instance variables (aka the variables inside __init__) from outside/inside the file without creating an instance of a class (i.e main = main.foo()).
Example:
class foo:
def __init__(self,name):
self.name = name
class bar:
os.mkdir(foo.name)
you can set the variable as global, and then you will be able to access this variable from everywhere. and also modifying it.
Not sure why you are using nested classes, but:
foo_name = None
class main:
class foo:
def __init__(self,name):
global foo_name
foo_name = self.name = name
class bar:
def __init__(self):
print(foo_name)
main.foo("Jonathan")
main.bar()
Prints out "Jonathan"

(Python) Should I use parameters or make it global? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
I have many functions that all share the same parameter. They will be inputting and outputting this parameter many times.
For example:
a = foo
a = fun(a)
a = bar(a)
def fun(a):
...
return a
def bar(a):
...
return a
What is more pro-grammatically correct, passing parameters through a function, or having it be globally accessible for all the functions to work with?
a = foo
fun()
bar()
def fun():
global a
...
def bar():
global a
...
The more localised your variables, the better.
This is virtually an axiom for any programming language.
structs in C (and equivalents in other languages such as FORTRAN) grew up from this realisation, and object orientated programming followed shortly after.
For re-usability of method, passing parameter is better way.
I agree with the other answers but just for the sake of completion, as others have pointed out a class sounds like a good idea here. Consider the following.
class myClass(object):
def __init__(self, foo):
self.a = foo
def fun(self):
# do stuff to self.a
def bar(self):
# do something else to self.a
c = myClass(foo)
c.fun()
c.bar()

How to stop a class halfway, to run the next class, and how to do so? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I searched on the internet and couldn't find a solution. Please help.
Let's say...
class A(self):
def __init__ (self):
"""one chunk of code here"""
if (condition met):
print 'access granted'
"""I want to stop the code here and ask it to run class B, instead of just one method from class B"""
else:
print 'Stop process'
break
class B(self):
def __init__ (self):
"""one more chunk of codes here"""
Is this possible? (pardon my mess of codes)
Your if condition code will run only once when you run that script. Whenever you create an instance of a class, only __init__ function is run. As interjay mentioned, you don't run a class, you run functions.

Python: naming of boolean/flag class attributes [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
Coding style question: What is the recommended way of naming flag class attributes, i.e. attributes being True or False. Styles I can think of are:
class MyClass:
def my_method(self):
self.request = False
class MyClass:
def my_method(self):
self.is_request = False
class MyClass:
def my_method(self):
self.request_flag = False
PEP8 does not seem to give a firm recommendation. Is there a canonical way of doing this?
Considering that booleans are mostly used in coditions, the second way seems most appropriate.
o = MyClass()
...
if o.is_request: # very intuitive
# it's a request

Categories

Resources