Question
Is there a naming convention I should follow with classes?
Answer
It’s ultimately up to you, but following a convention can help make your code look much cleaner overall. It also may end up depending on where you work because some companies enforce their own code conventions.
For most people, class names will be capitalized and all one word, unlike how we’ve been writing variables with underscores so far.
As a general rule, your class names should be as descriptive as possible, leaving as little room for ambiguity as possible. For example, naming a class MyClass
is totally fine, but tells me or anyone else reading your code absolutely nothing about what the class does. Naming a class HTMLPageTitleScraper
, on the other hand, tells me exactly what the class does!
5 Likes
When it says ‘created a lemon instance’, does this just mean ‘created a lemon variable’?
So, is a class like a large function, and it could be ‘called’ by other classes?
2 Likes
Genereally speaking it’s a larger function yeah.
But classes usually specify a specefic Object.
You can also nest classes inside of classes.
class A:
class B:
pass
pass
class car would be an object of 4 wheels motor drives ect
class type of car would be an inheritance of class car
1 Like
Just found a good code discription online to show classes calling classes
https://pythonspot.com/inner-classes/
#!/usr/bin/env python
class Human:
def __init__(self):
self.name = 'Guido'
self.head = self.Head()
self.brain = self.Brain()
class Head:
def talk(self):
return 'talking...'
class Brain:
def think(self):
return 'thinking...'
guido = Human()
print guido.name
print guido.head.talk()
print guido.brain.think()
9 Likes