Class Inheritance and Creation in Python [closed] - python

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.
Improve this question
When you create 2 classes in Python, does the second class always have to be a subclass or the child class? Is it possible to have two classes that have object as their parameters? Thanks!
class Bird(object):
def __init__(self, name):
self.name = name
print("A %s has feathers" % self.name)
class Seagull(object):
def __init__(self):
print("Seagulls can fly")
super().__init__('Seagull')
seagull = Seagull()
What is wrong with this code? It says that Seagull is an inheritance so its (object) should be Bird... but why?

Whether you have subclasses or independent classes depends on the logic you're implementing.
With a bird and a seagull, you'd probably want a subclass, because a seagull is a kind of bird:
class Bird(object):
...
class Seagull(Bird):
...
In other situations, you would want separate classes, not related to each other:
class Bird(object):
...
class Locomotive(object):
...
By the way, in Python 3 the (object) part is not needed when it's literally object, so we would normally write:
class Bird:
...
class Seagull(Bird):
...
class Locomotive:
...

Related

Can you access attributes that were created with the init method from other methods in the class? [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 1 year ago.
Improve this question
When I write the __init__ method and assign attributes, can I access those attributes in other methods (functions) that I write in that class? If so, how is it done?
I've googled this but couldn't find an answer. I Haven't been able to wrap my head around this one.
Use self:
class MyClass:
def __init__(self):
self.name = 'John'
def other_method(self):
print(self.name)
other_method will print "John".
When you make a class and set an instance (like first_class = MyClass()) the def __init__(self): is run or initialised. Any variables in there, like self.name are able to be accessed from within the class and its functions, as well as when you use a class in another program. self kinda attaches that variable to that class.
Basically using Allure's example:
class MyClass:
def __init__(self):
self.name = "John"
def show_name(self):
print(self.name)
Then use MyClass's name outside of class, in a program:
firstClass = MyClass()#Initialise MyClass and its variables
print(firstClass.name)
Or:
firstClass= MyClass()
firstClass.show_name()
Both output:
'John'
(still putting up this answer for others, hope you don't mind :) )

What is difference between these two codes? [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 1 year ago.
Improve this question
Turtle=Animal("Turtle")
Turtle.set_category("reptile")
and
class Turtle(Animal):
category="reptile"
While learning object composition in python i came across a problem in which latter worked but former did not.
this was the class
class Animal:
name = ""
category = ""
def __init__(self, name):
self.name = name
def set_category(self, category):
self.category = category
These two sequences should behave the same:
turtle=Animal("Turtle")
turtle.set_category("reptile")
and
class Turtle(Animal):
category="reptile"
name = "Turtle"
turtle = Turtle()
The two turtle objects will behave identically.
In the first piece of code you are defining an instance of the Animal class stored in the Turtle variable, whereas in the second piece of code you are defining a new class called Turtle that will inherit from the Animal class.
Read more about class objects here: https://docs.python.org/3/tutorial/classes.html#class-objects
Read more about class inheritance here: https://docs.python.org/3/tutorial/classes.html#inheritance

class A :, class A(object) :, or class A() : [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 2 years ago.
Improve this question
What is the exact difference among these three and when do we use what?
class A:
pass
class A(object):
pass
or
class A():
pass
class A:
pass
It is implicitly subclass of object (as in other cases). I think it's most prefereable in case you don't inherit from anything (but it can depend on coding standards).
2)
class A(object):
pass
It is most explicit version.
3)
class A():
pass
In this case, as no class passed as parent class, by default it inherits from object.
So from functionality point of view, there is no difference. In Python3, all clasess inherit from object (even if it's not explixitly declared).
However, if you are using Python2, you need to pass superclass explicitly in every case.

calling __init__ method in composition [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 6 years ago.
Improve this question
I am trying to use composition in python but I wasn’t sure how to do it if I needed the __init__ method from the other class. In other words, how do I do this super(childClass, self).__init__(…) with composition?
for example:
class Class*(object):
….
class ClassA(Class*):
def __init__(self, lst, ...):
super(ClassA, self).__init__(...)
def meth1()
def meth2()
class ClassB(Class*):
def __init__(self, name, lst=None, other=None)
**here I want to use the __init__ method from ClassA**
self.a = ClassA(..)
def meth1():
return self.a.meth1()
I hope this is clear. Thanks!
In a composition, your ClassB simply has a member that happens to be a ClassA, so, you don't invoke ClassA's __init__ method directly, you just instantiate it if you need to.
class ClassB(Class*):
def __init__(self, name, lst=None, other=None):
self.my_A = ClassA(lst, ...)

Can I add more functionality to a class in python? [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 8 years ago.
Improve this question
If one has a partial class definition like this...
class Atom(object):
def _init_(self, id, mass = 0, pos = None, radius = 0):
self.id = id
self.name = name
self.mass = 0
Could one add the self.name part and still have it represent the class?
If you want your class to have the variable, name, you have to pass it in with the other arguments.
class Atom(object):
def _init_(self, id=None, name=None, mass=0, pos=None, radius=0):
self.id = id
self.name = name
...
Then you can access/assign it inside your class. Otherwise if you are just trying to get the name of the class it can be done without the need of assigning it, like so:
a = Atom()
a.__class__.__name__
>>> 'Atom'
Since python classes provide all the standard features of Object Oriented Programming: the class inheritance mechanism allows multiple base classes, a derived class can override any methods of its base class or classes, and a method can call the method of a base class with the same name :
Yes you can add more functionality to a class in python.

Categories